目标受众
本教程适用于那些在编程和机器学习方面有一定经验,并想要学习 TensorFlow 的人。例如:一位想在机器学习课程的最后一个项目中使用 TensorFlow 的计算机科学专业的学生;一位刚被分配到涉及深度学习项目的软件工程师;或是一位处于困惑中的新的 Google AI Resident 新手(向过去的 Jacob 大声打招呼)。如果你想进一步了解基础知识,请参阅以下资源:
输出:
3Traceback (most recent call last):...InvalidArgumentError (see above for traceback): You must feed a value *for* placeholder tensor 'Placeholder_2' with dtype int32 [[Node: Placeholder_2 = Placeholder[dtype=DT_INT32, shape=<unknown>, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]复制代码
代码:
import tensorflow as tf### build the graph## first set up the parametersm = tf.get_variable("m", [], initializer=tf.constant_initializer(0.))b = tf.get_variable("b", [], initializer=tf.constant_initializer(0.))init = tf.global_variables_initializer()## then set up the computationsinput_placeholder = tf.placeholder(tf.float32)output_placeholder = tf.placeholder(tf.float32)x = input_placeholdery = output_placeholdery_guess = m * x + bloss = tf.square(y - y_guess)## finally, set up the optimizer and minimization nodeoptimizer = tf.train.GradientDescentOptimizer(1e-3)train_op = optimizer.minimize(loss)### start the sessionsess = tf.Session()sess.run(init)### perform the training loop*import* random## set up problemtrue_m = random.random()true_b = random.random()*for* update_i *in* range(100000): ## (1) get the input and output input_data = random.random() output_data = true_m * input_data + true_b ## (2), (3), and (4) all take place within a single call to sess.run()! _loss, _ = sess.run([loss, train_op], feed_dict={input_placeholder: input_data, output_placeholder: output_data}) *print* update_i, _loss### finally, print out the values we learned for our two variables*print* "True parameters: m=%.4f, b=%.4f" % (true_m, true_b)*print* "Learned parameters: m=%.4f, b=%.4f" % tuple(sess.run([m, b]))复制代码
代码:
import tensorflow as tftwo_node = tf.constant(2)three_node = tf.constant(3)sum_node = two_node + three_node### this new copy of two_node is not on the computation path, so nothing prints!print_two_node = tf.Print(two_node, [two_node, three_node, sum_node])sess = tf.Session() print sess.run(sum_node)复制代码