import csv import random import tensorflow as tf import os column_names = [] train_feature=[] train_label=[] test_feature=[] test_label=[] with open("test.csv","r") as r: reader = csv.reader(r) for i in reader: rand = random.randint(0,100) if rand<=85: train_label.append((eval(i.pop()))) b = [eval(j) for j in i] train_feature.append(b) else: test_label.append((eval(i.pop()))) b = [eval(j) for j in i] test_feature.append(b) train_features = tf.constant(train_feature) train_labels = tf.constant(train_label) test_features = tf.constant(test_feature) test_labels = tf.constant(test_label) model = tf.keras.Sequential([ tf.keras.layers.Flatten(input_shape=(252,)), tf.keras.layers.Dense(10, activation=tf.nn.relu), # input shape required tf.keras.layers.Dense(10, activation=tf.nn.relu), tf.keras.layers.Dense(6) ]) predictions = model(train_features) tf.nn.softmax(predictions[:5]) print("Prediction: {}".format(tf.argmax(predictions, axis=1))) print(" Labels: {}".format(train_labels)) loss_object = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True) def loss(model, x, y, training): # training=training is needed only if there are layers with different # behavior during training versus inference (e.g. Dropout). y_ = model(x, training=training) return loss_object(y_true=y, y_pred=y_) l = loss(model, train_features, train_labels, training=False) def grad(model, inputs, targets): with tf.GradientTape() as tape: loss_value = loss(model, inputs, targets, training=True) return loss_value, tape.gradient(loss_value, model.trainable_variables) optimizer = tf.keras.optimizers.SGD(learning_rate=0.01) loss_value, grads = grad(model, train_features, train_labels) print("Step: {}, Initial Loss: {}".format(optimizer.iterations.numpy(), loss_value.numpy())) optimizer.apply_gradients(zip(grads, model.trainable_variables)) print("Step: {}, Loss: {}".format(optimizer.iterations.numpy(), loss(model, train_features, train_labels, training=True).numpy())) ## Note: Rerunning this cell uses the same model variables # Keep results for plotting train_loss_results = [] train_accuracy_results = [] num_epochs = 351 for epoch in range(num_epochs): epoch_loss_avg = tf.keras.metrics.Mean() epoch_accuracy = tf.keras.metrics.SparseCategoricalAccuracy() # Training loop - using batches of 32 branch = 32 for num in range(len(train_features)//branch): x = train_features[num*branch:(num+1)*branch] y = train_labels[num*branch:(num+1)*branch] # Optimize the model loss_value, grads = grad(model, x, y) optimizer.apply_gradients(zip(grads, model.trainable_variables)) # Track progress epoch_loss_avg.update_state(loss_value) # Add current batch loss # Compare predicted label to actual label # training=True is needed only if there are layers with different # behavior during training versus inference (e.g. Dropout). epoch_accuracy.update_state(y, model(x, training=True)) # End epoch train_loss_results.append(epoch_loss_avg.result()) train_accuracy_results.append(epoch_accuracy.result()) if epoch % 50 == 0: print("Epoch {:03d}: Loss: {:.3f}, Accuracy: {:.3%}".format(epoch, epoch_loss_avg.result(), epoch_accuracy.result())) test_accuracy = tf.keras.metrics.Accuracy() model.save("model") for num in range(len(test_features)): # training=False is needed only if there are layers with different # behavior during training versus inference (e.g. Dropout). x = test_features[num:num+1] y = test_labels[num:num+1] logits = model(x, training=False) prediction = tf.argmax(logits, axis=1, output_type=tf.int32) print("真实值为",y,"预测值为",prediction) test_accuracy(prediction, y) print("Test set accuracy: {:.3%}".format(test_accuracy.result()))