创建一个NN
import tensorflow as tf
import numpy as np
#fake data
x = np.linspace(-1, 1, 100)[:, np.newaxis] #shape(100,1)
noise = np.random.normal(0, 0.1, size=x.shape)
y = np.power(x, 2) + noise #shape(100,1) + noise
tf_x = tf.placeholder(tf.float32, x.shape) #input x
tf_y = tf.placeholder(tf.float32, y.shape) #output y
l = tf.layers.dense(tf_x, 10, tf.nn.relu) #hidden layer
o = tf.layers.dense(l, 1) #output layer
loss = tf.losses.mean_squared_error(tf_y, o ) #compute loss
train_op = tf.train.GradientDescentOptimizer(learning_rate=0.5).minimize(loss)
1.使用save对模型进行保存
sess= tf.Session()
sess.run(tf.global_variables_initializer()) #initialize var in graph
saver = tf.train.Saver() # define a saver for saving and restor

最低0.47元/天 解锁文章
780





