为什么我无法获得价值?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为什么我无法获得价值?相关的知识,希望对你有一定的参考价值。
[当您启动该程序时,您会看到按钮btnChange不需要输入。我是python的初学者,不知道出了什么问题。也许有人知道并且可以提供帮助吗?
%matplotlib
import tkinter
from tkinter import *
from tkinter import ttk
import tensorflow as tf
import numpy as np
import sys
import matplotlib.pyplot as 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()
答案
我理解。我只能从条目中获得3或5之类的数字。但是如何使用数学函数来实现呢?
以上是关于为什么我无法获得价值?的主要内容,如果未能解决你的问题,请参考以下文章