直接看代码,更容易理解
import tensorflow as tf
import numpy as np
graph = tf.Graph()
with graph.as_default():
x = tf.placeholder(dtype=tf.float32, shape=[None, 50], name='x_input')
y = tf.placeholder(dtype=tf.float32, shape=[None, 50], name='y_label')
x_val = np.arange(-25., 25., 1., dtype=np.float32).reshape([1, 50])
y_val = np.square(x_val)
with tf.variable_scope('net', reuse=tf.AUTO_REUSE):
l1 = tf.layers.dense(x, 100, activation=tf.nn.relu, name='layer1')
l2 = tf.layers.dense(l1, 100, activation=tf.nn.relu, name='layer2')
out = tf.layers.dense(l2, 50, name='output')
loss = tf.losses.mean_squared_error(y, out)
optimizer = tf.train.AdamOptimizer(0.001)
train_op = optimizer.minimize(loss)
init = tf.global_variables_initializer()
with tf.Session(graph=graph) as sess:
writer