当您启动该程序时,您会看到btnChange按钮从输入时就不会起作用。我是python的初学者,不知道出了什么问题。也许有人知道并且可以提供帮助吗? %matplotlib 导入tkinter 从tkinter导入* 从tkinter导入ttk 将tensorflow作为tf导入 将numpy导入为np 导入系统 导入matplotlib.pyplot作为plt
def target_func(x):
return np.sin(x)
#sin(x)-done && loss_val<=0.001
#cos(x)-done && loss_val<=0.001
#1 / (1 + np.exp(-1 * x))*(np.cos(x) - np.sin(x))-done && #loss_val<=0.0001
#x**2 - done && loss_val<=0.02
def inference(inputs):
fc1 = tf.layers.dense(inputs=inputs, units=10, activation=tf.nn.relu, name="fc1")
fc2 = tf.layers.dense(inputs=fc1, units=10, activation=tf.nn.relu, name="fc2")
output = tf.layers.dense(inputs=fc2, units=1, activation=None, name="output")
return output
def loss(truth, predict):
losses = tf.reduce_sum(tf.square(truth-predict, name="loss"))
return losses
def training(losses):
return tf.train.AdamOptimizer().minimize(losses)
def main(argv=None):
x = tf.placeholder(tf.float64, shape=(None, 1), name='inputs')
y = tf.placeholder(tf.float64, shape=(None, 1), name='truth')
batch_size = 5
predict = inference(x)
losses = loss(y, predict)
train_step = training(losses)
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(100000):
inp = (np.random.rand(batch_size, 1)-0.5)*10
sess.run(train_step, feed_dict={x: inp, y: target_func(inp)})
if i % 1000 == 0:
inp = (np.random.rand(batch_size, 1)-0.5)*10
loss_val = sess.run(losses, feed_dict={x: inp, y: target_func(inp)})
print ('Step:%d, Loss:%f' % (i, loss_val))
if(loss_val<=0.001):
system.exit()
tkMessageBox.showinfo("ValueError!")
if i % 10000 == 0:
rang = np.arange(-np.pi, np.pi, 0.1)
rang2 = np.reshape(rang, (-1, 1))
truth = target_func(rang)
pred = sess.run(predict, feed_dict={x: rang2})
plt.figure()
plt.plot(rang, truth)
plt.plot(rang, pred)
plt.ion()
plt.pause(2)
window = Tk()
window.title("Approximation")
window.resizable(0, 0)
window.geometry('350x200')
#For functions
lbl4=Label(window,text = "Write your function")
lbl4.grid(column = 0,row = 3)
v = DoubleVar ()
txt4=Entry(window,textvariable = v,width= 10)
txt4.grid(column=1,row=3)
s = v.get ()
btnChange=Button(window,text="Save function",command=lambda:target_func(s))
btnChange.grid(column =2,row = 2)
btn = Button(window, text="Start", command=lambda:main())
btn.grid(column=4, row=4)
window.mainloop()
if __name__ == '__main__':
main()