import tensorflow as tf
w1 = tf.Variable(tf.random_normal([2,3],stddev=1,seed=1))
w2 = tf.Variable(tf.random_normal([3,1],stddev=1,seed=1))
# 定义placeholder作为存放输入数据的地方,这里的维度也不一定要定义
# 但是如果维度是确定,那么给出维度可以降低出错的概率
x = tf.placeholder(tf.float32,shape=(1,2),name='input')
a = tf.matmul(x,w1)
y = tf.matmul(a,w2)
sess = tf.Session()
init_op = tf.global_variables_initializer()
sess.run(init_op)
print(sess.run(y,feed_dict={x:[[0.7,0.9]]}))
>>> print(sess.run(y,feed_dict={x:[[0.7,0.9]]}))
[[ 3.95757794]]
利用placeholder定义了一个位置,这个位置是喂数据
#使用sigmoid函数将y转换成0~1之间的数值
y = tf.sigmoid(y)
# 定义损失函数来刻画预测值和真实值的差距
# 交叉熵
# tf.clip_by_value是对对数据的限制。
#当输出小于1e-10时,输出1e-10;
#当输出大于1e-10小于1.0时,输出原值;
#当输出大于1.0时,输出1.0;
cross_entropy = -tf.reduce_mean(y_*tf.log(tf.clip_by_value(y,1e-10,1.0))+(1-y)*tf.log(tf.clip_by_value(1-y,1e-10,1.0)))
#定义学习率
learning_rate = 0.001
#定义反向传播算法来优化神经网络中的参数
tf.train.AdadeltaOptimizer(learning_rate).minimize(cross_entropy)
完整代码示例
import tensorflow as tf
# 利用numpy生成数据集
from numpy.random import RandomState
# 定义训练集的barch大小
batch_size = 8
# 定义神经网络的参数
w1 = tf.Variable(tf.random_normal([2,3],stddev = 1,seed=1))
w2 = tf.Variable(tf.random_normal([3,1],stddev = 1,seed=1))
# 在shape的一个维度上使用None可以方便使用不同规模大小的batch
x = tf.placeholder(tf.float32,shape=(None,2),name='x_input') #不指定行数只指定输入列数
y_ = tf.placeholder(tf.float32,shape=(None,1),name='y_input')
#定义神经网络的前向传播过程
a = tf.matmul(x,w1)
y = tf.matmul(a,w2)
# 定义损失函数和反向传播算法
y = tf.sigmoid(y)
loss_function = -tf.reduce_mean(y_*tf.log(tf.clip_by_value(y,1e-10,1.0))+(1-y_)*tf.log(tf.clip_by_value(1-y,1e-10,1.0)))
train_step = tf.train.AdamOptimizer(0.001).minimize(loss_function)
# 通过随机数生成一个模拟数据集
rdm = RandomState(1)
dataset_size = 128
X = rdm.rand(dataset_size,2)
Y = [[int(x1+x2<1)] for (x1,x2) in X]
with tf.Session() as sess:
init_op = tf.global_variables_initializer()
sess.run(init_op)
print(sess.run(w1))
print(sess.run(w2))
'''
[[-0.81131822 1.48459876 0.06532937] [-2.4427042 0.0992484 0.59122431]]
[[-0.81131822] [ 1.48459876] [ 0.06532937]]
'''
# 设定训练的轮数
STEPS = 5000
for i in range(STEPS):
start = (i*batch_size) % dataset_size
end = min(start+batch_size,dataset_size)
sess.run(train_step,feed_dict={x: X[start:end],y_:Y[start:end]})
if i % 1000 == 0:
total_loss_function = sess.run(loss_function,feed_dict={x:X,y_:Y})
print("After %d training steps, loss function on all data is %g"%(i,total_loss_function))
'''
After 0 training steps, loss function on all data is 1.89805
After 1000 training steps, loss function on all data is 0.655075
After 2000 training steps, loss function on all data is 0.626172
After 3000 training steps, loss function on all data is 0.615096
After 4000 training steps, loss function on all data is 0.610309
'''
print(sess.run(w1))
print(sess.run(w2))
'''
[[ 0.02476983 0.56948674 1.6921941 ] [-2.19773483 -0.23668921 1.11438954]]
[[-0.45544702] [ 0.49110925] [-0.98110336]]
'''