写python代码:利用工具包如TensorFlow来获取LeNet-5的网络参数,并整理成1维的形式
时间: 2024-05-29 10:09:04 浏览: 13
好的,以下是获取LeNet-5网络参数并整理成1维形式的Python代码:
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
# 读取MNIST数据集
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
# 构建LeNet-5模型
def LeNet5(x, is_training):
# 输入层
x = tf.reshape(x, [-1, 28, 28, 1])
# 第一个卷积层
conv1 = tf.layers.conv2d(x, 6, 5, strides=1, padding='valid', activation=tf.nn.relu)
pool1 = tf.layers.max_pooling2d(conv1, 2, 2)
# 第二个卷积层
conv2 = tf.layers.conv2d(pool1, 16, 5, strides=1, padding='valid', activation=tf.nn.relu)
pool2 = tf.layers.max_pooling2d(conv2, 2, 2)
# 全连接层
fc1 = tf.layers.flatten(pool2)
fc1 = tf.layers.dense(fc1, 120, activation=tf.nn.relu)
fc2 = tf.layers.dense(fc1, 84, activation=tf.nn.relu)
logits = tf.layers.dense(fc2, 10)
return logits
# 计算LeNet-5的网络参数
with tf.Session() as sess:
inputs = tf.placeholder(tf.float32, [None, 784])
logits = LeNet5(inputs, False)
variables = [var for var in tf.global_variables() if 'LeNet5' in var.name]
sess.run(tf.global_variables_initializer())
variables_val = sess.run(variables, feed_dict={inputs: mnist.train.images[:1]})
# 整理网络参数成1维形式
params = []
for var_val in variables_val:
var_val_1d = var_val.reshape(-1)
params.extend(var_val_1d.tolist())
print(params)
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)