I wanted to make own neural network for Speech data set and for that using tensorflow.I am writing the code imported library and dataset then done one hot encoding and after all done the weights and baises assignment and then done the forward propagation with the random values and for back propagation and cost minimization used a loss function to do so but unable to use the optimizer dont know why.
tf.compat.v1.disable_v2_behavior()
X = tf.compat.v1.placeholder(tf.float32, [None, n_dim])
Y = tf.compat.v1.placeholder(tf.float32, [None, n_classes])
W_1 = tf.Variable(tf.random.normal([n_dim, n_hidden_units_one], mean=0, stddev=sd))
b_1 = tf.Variable(tf.random.normal([n_hidden_units_one], mean=0, stddev=sd))
h_1 = tf.nn.tanh(tf.matmul(X, W_1) + b_1)
W_2 = tf.Variable(tf.random.normal([n_hidden_units_one, n_hidden_units_two], mean=0, stddev=sd))
b_2 = tf.Variable(tf.random.normal([n_hidden_units_two], mean=0, stddev=sd))
h_2 = tf.nn.sigmoid(tf.matmul(h_1, W_2) + b_2)
W = tf.Variable(tf.random.normal([n_hidden_units_two, n_classes], mean=0, stddev=sd))
b = tf.Variable(tf.random.normal([n_classes], mean=0, stddev=sd))
y_ = tf.nn.softmax(tf.matmul(h_2, W) + b)
init = tf.compat.v1.global_variables_initializer()
saver = tf.compat.v1.train.Saver()
cost_function = tf.reduce_mean(-tf.reduce_sum(Y * tf.math.log(y_), axis=[1]))
tape_df = tf.GradientTape(persistent=True)
opt = tf.optimizers.SGD(learning_rate=0.01)
optimizer = opt.minimize(cost_function, var_list=[W, b], tape =tape_df)
But I am getting this error:
Traceback (most recent call last):
File "D:/Sound Data/Sound-classification-on-Raspberry-Pi-with-Tensorflow/trainModel.py", line 134, in <module>
optimizer = opt.minimize(cost_function, var_list=[W, b], tape =tape_df)
File "D:\Sound Data\Sound-classification-on-Raspberry-Pi-with-Tensorflow\venv\lib\site-packages\keras\optimizers\optimizer_experimental\optimizer.py", line 526, in minimize
grads_and_vars = self.compute_gradients(loss, var_list, tape)
File "D:\Sound Data\Sound-classification-on-Raspberry-Pi-with-Tensorflow\venv\lib\site-packages\keras\optimizers\optimizer_experimental\optimizer.py", line 259, in compute_gradients
grads = tape.gradient(loss, var_list)
File "D:\Sound Data\Sound-classification-on-Raspberry-Pi-with-Tensorflow\venv\lib\site-packages\tensorflow\python\eager\backprop.py", line 1052, in gradient
raise RuntimeError("A non-persistent GradientTape can only be used to "
RuntimeError: A non-persistent GradientTape can only be used to compute one set of gradients (or jacobians)
How can I solve this error? I am using Tensorflow 2.11 version.