代码:
#author@chengxiaona
import tensorflow as tf
import numpy as np
#输入训练数据,这里是python的list, 也可以定义为numpy的ndarray
x_data = [[1., 0.], [0., 1.], [0., 0.], [1., 1.]]
#定义占位符,占位符在运行图的时候必须feed数据
x = tf.placeholder(tf.float32, shape = [None, 2])
#训练数据的标签,注意维度
y_data = [[1], [1], [0], [0]]
y = tf.placeholder(tf.float32, shape = [None, 1])
#定义variables,在运行图的过程中会被按照优化目标改变和保存
weights = {
'w1': tf.Variable(tf.random_normal([2, 16])), 'w2': tf.Variable(tf.random_normal([16, 1]))}
bias = {
'b1': tf.Variable(tf.zeros([1])), 'b2': tf.Variable(tf.zeros([1]))}

这篇博客通过代码展示了如何利用TensorFlow构建一个神经网络来实现异或门功能。作者指出,多维矩阵与一维矩阵相加不会产生维度错误,并强调在TensorFlow中,变量的类型与是否在Session内无关,fetch的变量必须是tensor或string,而feed的数据类型可以是Python标量、字符串、列表、numpy数组等。代码实现部分受到了其他博主的启发。

2475

被折叠的 条评论
为什么被折叠?



