I am trying to do a simple fully connected single hidden layer neural network with MNIST datasets.
for epoch in range(training_epoch):
for step in range(total_step):
xs = x_train[step*batch_size:(step+1)*batch_size]
ys = y_train[step*batch_size:(step+1)*batch_size]
grads = grad(xs, ys, W, B)
optimizer.apply_gradients(zip(grads, [W, B]))
W and B are tf.Variable type:
Input_Dim = 784
H1_NN = 64
w1 = tf.Variable(tf.random.normal([Input_Dim, H1_NN], mean=0.0, stddev=1.), dtype=tf.float32)
b1 = tf.Variable(tf.zeros([H1_NN]), dtype=tf.float32)
Output_Dim = 10
w2 = tf.Variable(tf.random.normal([H1_NN, Output_Dim], mean=0.0, stddev=1.0), dtype=tf.float32)
b2 = tf.Variable(tf.zeros([Output_Dim]), dtype=tf.float32)
W = [w1, w2]
B = [b1, b2]enter code here
error:
Traceback (most recent call last): File "C:/Users/HSNE_LP22B/PycharmProjects/浙大城市学院/mnist手写数字识别_1隐藏层.py", line 91, in optimizer.apply_gradients( grads_and_vars) File "C:\Users\HSNE_LP22B\Anaconda3\envs\TF2.1\lib\site-packages\tensorflow_core\python\keras\optimizer_v2\optimizer_v2.py", line 434, in apply_gradients self._create_slots(var_list) File "C:\Users\HSNE_LP22B\Anaconda3\envs\TF2.1\lib\site-packages\tensorflow_core\python\keras\optimizer_v2\adam.py", line 149, in _create_slots self.add_slot(var, 'm') File "C:\Users\HSNE_LP22B\Anaconda3\envs\TF2.1\lib\site-packages\tensorflow_core\python\keras\optimizer_v2\optimizer_v2.py", line 574, in add_slot var_key = _var_key(var) File "C:\Users\HSNE_LP22B\Anaconda3\envs\TF2.1\lib\site-packages\tensorflow_core\python\keras\optimizer_v2\optimizer_v2.py", line 1065, in _var_key if var._in_graph_mode: AttributeError: 'list' object has no attribute '_in_graph_mode'
I tried with optimizer.apply_gradients(zip(grads, W+B)), but this eoor happens
tensorflow.python.framework.errors_impl.InvalidArgumentError: Shapes of all inputs must match: values[0].shape = [784,64] != values[1].shape = [64,10] [Op:Pack] name: packed