修改一下範例
x -> x0, x1....
從 1對1 變成 2 對 1,,,,,
import tensorflow as tf
# Model parameters
W0 = tf.Variable([.3], dtype=tf.float32)
W1 = tf.Variable([.3], dtype=tf.float32)
b = tf.Variable([-.3], dtype=tf.float32)
# Model input and output
x0 = tf.placeholder(tf.float32)
x1 = tf.placeholder(tf.float32)
linear_model = W0*x0 + W1*x1 + b
y = tf.placeholder(tf.float32)
# loss
loss = tf.reduce_sum(tf.square(linear_model - y)) # sum of the squares
# optimizer
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
# training data
x0_train = [1, 2, 3, 4]
x1_train = [1, 2, 3, 4]
y_train = [0, -1, -2, -3]
# training loop
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init) # reset values to wrong
for i in range(1000):
sess.run(train, {x0: x0_train,x1: x1_train, y: y_train})
# evaluate training accuracy
curr_W0,curr_W1, curr_b, curr_loss = sess.run([W0,W1, b, loss], {x0: x0_train, x1 : x1_train, y: y_train})
print("W0: %s W1: %s b: %s loss: %s"%(curr_W0,curr_W1, curr_b, curr_loss))
print(sess.run(linear_model, {x0: 3.5, x1 :3.5}))
------------------------------------------------------------------------------------------------------------------------
def model_fn(features, labels, mode):
# Build a linear model and predict values
W0 = tf.get_variable("W0", [1], dtype=tf.float64)
W1 = tf.get_variable("W1", [1], dtype=tf.float64)
b = tf.get_variable("b", [1], dtype=tf.float64)
y = W0*features['x0'] + W1*features['x1'] + b
# Loss sub-graph
loss = tf.reduce_sum(tf.square(y - labels))
# Training sub-graph
global_step = tf.train.get_global_step()
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = tf.group(optimizer.minimize(loss),
tf.assign_add(global_step, 1))
# EstimatorSpec connects subgraphs we built to the
# appropriate functionality.
return tf.estimator.EstimatorSpec(
mode=mode,
predictions=y,
loss=loss,
train_op=train)
estimator = tf.estimator.Estimator(model_fn=model_fn)
x0_train = np.array([1., 2., 3., 4.])
x1_train = np.array([1., 2., 3., 4.])
y_train = np.array([0., -1., -2., -3.])
x0_eval = np.array([2., 5., 8., 1.])
x1_eval = np.array([2., 5., 8., 1.])
y_eval = np.array([-1.01, -4.1, -7., 0.])
input_fn = tf.estimator.inputs.numpy_input_fn(
{"x0": x0_train,"x1": x1_train}, y_train, batch_size=4, num_epochs=None, shuffle=True)
train_input_fn = tf.estimator.inputs.numpy_input_fn(
{"x0": x0_train,"x1": x1_train}, y_train, batch_size=4, num_epochs=1000, shuffle=False)
eval_input_fn = tf.estimator.inputs.numpy_input_fn(
{"x0": x0_eval,"x1": x1_eval}, y_eval, batch_size=4, num_epochs=1000, shuffle=False)
# train
estimator.train(input_fn=input_fn, steps=1000)
# Here we evaluate how well our model did.
train_metrics = estimator.evaluate(input_fn=train_input_fn)
eval_metrics = estimator.evaluate(input_fn=eval_input_fn)
print("train metrics: %r"% train_metrics)
print("eval metrics: %r"% eval_metrics)
curr_W0,curr_W1, curr_b, curr_loss = sess.run([W0,W1, b, loss], {x0: x0_train, x1 : x1_train, y: y_train})
print("W0: %s W1: %s b: %s loss: %s"%(curr_W0,curr_W1, curr_b, curr_loss))
------------------------------------------------------------------------------------------
batch_ys
(100(列), 10(欄)) [[ 0. 1. 0. 0. 0. 0. 0. 0. 0. 0.] [ 0. 0. 0. 0. 0. 1. 0. 0. 0. 0.] [ 0. 0. 0. 1. 0. 0. 0. 0. 0. 0.] [ 1. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [ 0. 0. 0. 0. 0. 0. 0. 1. 0. 0.] [ 0. 0. 0. 0. 0. 1. 0. 0. 0. 0.] [ 0. 0. 0. 0. 0. 0. 1. 0. 0. 0.] [ 0. 0. 0. 0. 0. 1. 0. 0. 0. 0.] [ 0. 0. 0. 0. 0. 0. 0. 0. 1. 0.] [ 0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]
沒有留言:
張貼留言