?TensorFlow???BP?????????:
- ??????:
import tensorflow as tf
- ?????????????:
X = tf.placeholder(tf.float32, [None, input_size]) # ????????,None????????????? Y = tf.placeholder(tf.float32, [None, output_size]) # ????????
- ?????????:
W = tf.Variable(tf.random_normal([input_size, hidden_size])) # ???????????? b = tf.Variable(tf.random_normal([hidden_size])) # ???????? V = tf.Variable(tf.random_normal([hidden_size, output_size])) # ???????????? c = tf.Variable(tf.random_normal([output_size])) # ????????
- ?????????:
hidden_layer = tf.nn.sigmoid(tf.matmul(X, W) + b) # ?????? output_layer = tf.matmul(hidden_layer, V) + c # ??????
- ??????????:
loss = tf.reduce_mean(tf.square(Y - output_layer)) # ???????????? optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss) # ????????????????
- ????:
with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for epoch in range(num_epochs): _, cost = sess.run([optimizer, loss], feed_dict={X: train_X, Y: train_Y}) if epoch % display_step == 0: print("Epoch:", 'd' % (epoch+1), "cost=", "{:.9f}".format(cost)) print("Optimization Finished!") # ???? correct_prediction = tf.equal(tf.argmax(output_layer, 1), tf.argmax(Y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) print("Accuracy:", accuracy.eval({X: test_X, Y: test_Y}))
??????,?????TensorFlow???????BP?????????????????????,???????????????????????????