0

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.

asked Mar 14, 2023 at 1:37
3
  • if you're using tf 2.11 why are you using tf.compat.v1.placeholder? Commented Mar 14, 2023 at 13:20
  • I am using this code : github.com/GianlucaPaolocci/…. They are using Tensorflow 1. Commented Mar 14, 2023 at 14:10
  • You probably don't want to mix tf1 and 2, instead try implementing the code in tf2, which is much simpler than what you think tensorflow.org/guide/autodiff Commented Mar 14, 2023 at 22:33

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.