# 为什么我无法获得价值？

``````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):
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