import math import numpy as np import h5py import matplotlib.pyplot as plt import tensorflow as tf from tensorflow.python.framework import ops from tf_utils import load_dataset, random_mini_batches, convert_to_one_hot, predict %matplotlib inline np.random.seed(1)
1 2 3 4 5 6 7
y_hat = tf.constant(36, name="y_hat") y = tf.constant(39, name="y") loss = tf.Variable((y-y_hat)**2, name="loss") init = tf.global_variables_initializer() with tf.Session() as session: session.run(init) print(session.run(loss))
9
1 2 3 4
a=tf.constant(2, name="a") b=tf.constant(10, name="b") c=tf.multiply(a, b) print(c) #必须要创建并运行session
deflinear_function(): np.random.seed(1) X=tf.constant(np.random.randn(3,1), name="X") W=tf.constant(np.random.randn(4,3), name="W") b=tf.constant(np.random.randn(4,1), name="b") Y=tf.add(tf.matmul(W, X), b) sess = tf.Session() result = sess.run(Y) sess.close() return result
激活函数
1 2 3 4 5 6 7 8 9
defsigmoid(z): x = tf.placeholder(tf.float32, name="x") sigmoid = tf.sigmoid(x) with tf.Session() as sess: result = sess.run(sigmoid, feed_dict={x:z}) return result
WARNING:tensorflow:From <ipython-input-64-3b18fa0bc2c0>:5: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Future major versions of TensorFlow will allow gradients to flow
into the labels input on backprop by default.
See `tf.nn.softmax_cross_entropy_with_logits_v2`.
Cost after epoch 0: 1.895938
Cost after epoch 100: 1.400813
Cost after epoch 200: 1.255541
Cost after epoch 300: 1.181168
Cost after epoch 400: 1.114092
Cost after epoch 500: 1.073960
Cost after epoch 600: 1.032575
Cost after epoch 700: 0.977509
Cost after epoch 800: 0.937244
Cost after epoch 900: 0.886205
Cost after epoch 1000: 0.882042
Cost after epoch 1100: 0.824795
Cost after epoch 1200: 0.818317
Cost after epoch 1300: 0.787285
Cost after epoch 1400: 0.770850
Parameters have been trained!
Train Accuracy: 0.723148
Test Accuracy: 0.516667