深度学习中的单神经元与前馈神经网络实现
1. TensorFlow实现单神经元逻辑回归
TensorFlow实现单神经元逻辑回归并不困难,与线性回归的实现有很多相似之处。
首先,我们需要定义占位符和变量:
import tensorflow as tf
import numpy as np
# 假设n_dim已经定义
tf.reset_default_graph()
X = tf.placeholder(tf.float32, [n_dim, None])
Y = tf.placeholder(tf.float32, [1, None])
learning_rate = tf.placeholder(tf.float32, shape=())
W = tf.Variable(tf.zeros([1, n_dim]))
b = tf.Variable(tf.zeros(1))
init = tf.global_variables_initializer()
上述代码与线性回归模型的代码相同,但我们需要定义不同的成本函数和神经元输出(使用sigmoid函数):
y_ = tf.sigmoid(tf.matmul(W,X)+b)
cost = - tf.reduce_mean(Y * tf.log(y_)+(1-Y) * tf.log(1-y_))
training_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
超级会员免费看
订阅专栏 解锁全文

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



