TensorFlow学习手册(一)——FC基本网络架构(一)

TensorFlow学习手册系列:

1.FC基本网络架构(一)

2.FC基本网络架构(二)

 

开始更代码篇了,先上手TensorFlow。我会将原来博文进行整合,后期更博了就更新目录。

 

首先是基本的神经元框架:

激活函数的常用配置为 sigmod 函数,使值在-1到1的范围内

                                   其他还有Rule等等。

偏置项相当于一个调节系数。

损失函数,成本函数也需要有所了解

 

然后是一个神经网络的八股图,具体代码附上

 

 

接下来是我运行这段代码的结果,主要用的是梯度下降和MSE的方法,可以改反向传播的方式等等

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是使用 TensorFlow 设计一个卷积神经网络识别 MNIST 手写数字的代码。 首先,我们需要导入 TensorFlow 和 MNIST 数据集: ```python import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) ``` 然后,我们定义卷积神经网络的输入和输出。输入是一个 28x28 的灰度图像,输出是一个长度为 10 的向量,表示对应的数字的概率: ```python x = tf.placeholder(tf.float32, [None, 784]) y = tf.placeholder(tf.float32, [None, 10]) ``` 接下来,我们定义卷积神经网络的结构。这里我们使用两个卷积层和两个池化层,然后连接两个全连接层。具体的结构如下: - 第一个卷积层:32 个 5x5 的卷积核,步长为 1,使用 ReLU 激活函数。 - 第一个池化层:2x2 的池化核,步长为 2。 - 第二个卷积层:64 个 5x5 的卷积核,步长为 1,使用 ReLU 激活函数。 - 第二个池化层:2x2 的池化核,步长为 2。 - 第一个全连接层:1024 个神经元,使用 ReLU 激活函数。 - 第二个全连接层:10 个神经元,使用 Softmax 激活函数。 ```python x_image = tf.reshape(x, [-1, 28, 28, 1]) # 第一个卷积层 W_conv1 = tf.Variable(tf.truncated_normal([5, 5, 1, 32], stddev=0.1)) b_conv1 = tf.Variable(tf.constant(0.1, shape=[32])) h_conv1 = tf.nn.relu(tf.nn.conv2d(x_image, W_conv1, strides=[1, 1, 1, 1], padding='SAME') + b_conv1) # 第一个池化层 h_pool1 = tf.nn.max_pool(h_conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') # 第二个卷积层 W_conv2 = tf.Variable(tf.truncated_normal([5, 5, 32, 64], stddev=0.1)) b_conv2 = tf.Variable(tf.constant(0.1, shape=[64])) h_conv2 = tf.nn.relu(tf.nn.conv2d(h_pool1, W_conv2, strides=[1, 1, 1, 1], padding='SAME') + b_conv2) # 第二个池化层 h_pool2 = tf.nn.max_pool(h_conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') # 第一个全连接层 W_fc1 = tf.Variable(tf.truncated_normal([7 * 7 * 64, 1024], stddev=0.1)) b_fc1 = tf.Variable(tf.constant(0.1, shape=[1024])) h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64]) h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) # 第二个全连接层 W_fc2 = tf.Variable(tf.truncated_normal([1024, 10], stddev=0.1)) b_fc2 = tf.Variable(tf.constant(0.1, shape=[10])) y_pred = tf.nn.softmax(tf.matmul(h_fc1, W_fc2) + b_fc2) ``` 接下来,我们定义损失函数和优化器。这里我们使用交叉熵作为损失函数,使用 Adam 优化器进行梯度下降: ```python cross_entropy = tf.reduce_mean(-tf.reduce_sum(y * tf.log(y_pred), reduction_indices=[1])) train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) ``` 最后,我们定义评估模型的方法。我们使用准确率作为评估指标: ```python correct_prediction = tf.equal(tf.argmax(y_pred, 1), tf.argmax(y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) ``` 现在,我们可以开始训练模型了。我们先初始化 TensorFlow 的会话,并进行 10000 次迭代,每迭代 100 次就输出一次模型在验证集上的准确率: ```python sess = tf.Session() sess.run(tf.global_variables_initializer()) for i in range(10000): batch = mnist.train.next_batch(50) if i % 100 == 0: train_accuracy = accuracy.eval(session=sess, feed_dict={x: batch[0], y: batch[1]}) print("step %d, training accuracy %g" % (i, train_accuracy)) train_step.run(session=sess, feed_dict={x: batch[0], y: batch[1]}) print("test accuracy %g" % accuracy.eval(session=sess, feed_dict={x: mnist.test.images, y: mnist.test.labels})) ``` 完整的代码如下所示:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值