#!/usr/bin/python
from __future__ import print_function
import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = ''
node1 = tf.constant(3.0, dtype=tf.float32)
node2 = tf.constant(4.0) # node2 dtype also equal tf.float32 implicitly
print(node1, node2)
sess = tf.Session() # SSSession
a = tf.placeholder(tf.float32) # A placeholder is a promise to provide a value later
b = tf.placeholder(tf.float32)
adder_node = a + b
print(sess.run(adder_node, {a: , b: 4.5})) # fetches=a, feed_dict=dict
print(sess.run(adder_node, {a: [, ], b: [, ]})) # feed_dict=tuple
W = tf.Variable([.], dtype=tf.float32)
b = tf.Variable([-.], dtype=tf.float32)
x = tf.placeholder(tf.float32)
linear_model = W * x + b
init = tf.global_variables_initializer() # tf.Variable must be explicitly initialize, tf.constant
sess.run(init)
print(sess.run(linear_model, {x: [, , , ]})) # while x=, x=, … linear_model = ?
y = tf.placeholder(tf.float32) # desired values
squared_deltas = tf.square(linear_model - y) # creates a vector of error delta
loss = tf.reduce_sum(squared_deltas) # create a single scalar that abstracts the error of all examples
print(sess.run(loss, {x: [, , , ], y: [, -, -, -]}))
fixW = tf.assign(W, [-.]) # tf.assign change initialized Variable value
fixb = tf.assign(b, [.])
sess.run([fixW, fixb])
print(sess.run(loss, {x: [, , , ], y: [, -, -, -]}))
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
sess.run(init)
for i in range():
sess.run(train, {x: [, , , , ], y: [, -, -, -]})
print(sess.run([W, b]))
###########################
W = tf.Variable([.], dtype=tf.float32)
b = tf.Variable([-.], dtype=tf.float32)
x = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32)
linear_model = W * x + b
loss = tf.reduce_sum(tf.square(linear_model - y))
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
x_train = [, , , ]
y_train = [, -, -, -]
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
for i in range():
sess.run(train, {x: x_train, y: y_train})
curr_W, curr_b, curr_loss = sess.run([W, b, loss], {x: x_train, y: y_train})
print("W: %s b: %s loss: %s" % (curr_W, curr_b, curr_loss))
#########################
##########################
import numpy as np
feature_columns = [tf.feature_column.numeric_column("x", shape=[])]
estimator = tf.estimator.LinearRegressor(feature_columns=feature_columns)
x_train = np.array([., ., ., .])
y_train = np.array([., -., -., -.])
x_eval = np.array([., ., ., .])
y_eval = np.array([-1.01, -4.1, -, .])
input_fn = tf.estimator.inputs.numpy_input_fn(
{"x": x_train}, y_train, batch_size=, num_epochs=None, shuffle=True
)
train_input_fn = tf.estimator.inputs.numpy_input_fn(
{"x": x_train}, y_train, batch_size=, num_epochs=, shuffle=False
)
eval_input_fn = tf.estimator.inputs.numpy_input_fn(
{"x": x_train}, y_train, batch_size=, num_epochs=, shuffle=False
)
estimator.train(input_fn=input_fn, steps=)
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)
#######################
手机扫一扫
移动阅读更方便
你可能感兴趣的文章