TF girls系列(3)之CNN网络的搭建

本文详细介绍了CNN网络的具体结构,包括卷积层、池化层、全连接层等,并通过实例展示了如何利用TensorFlow搭建CNN网络。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CNN的网络结构:
conv+relu–> conv+relu+pool–>conv+relu–> conv+relu+pool–>fc+relu–>fc
在这里插入图片描述

# -*- coding: utf-8 -*-
"""
Created on Wed Mar 27 19:38:44 2019

@author: macheng
"""

from __future__ import print_function, division
import tensorflow as tf
from sklearn.metrics import confusion_matrix
import numpy as np
import load

#train_samples 是维度为[num_images, image_size, image_size, num_channels]大小的4维矩阵
#train_labels 是维度为[num_images, 10]大小的2维矩阵
train_samples, train_labels = load._train_samples, load._train_labels
test_samples, test_labels = load._test_samples,  load._test_labels

print('Training set', train_samples.shape, train_labels.shape)
print('    Test set', test_samples.shape, test_labels.shape)

image_size = load.image_size       #32
num_labels = load.num_labels       #10
num_channels = load.num_channels   #channel为1,灰度图


def get_chunk(samples, labels, chunkSize):

	if len(samples) != len(labels):
		raise Exception('Length of samples and labels must equal')
	stepStart = 0  # initial step
	i = 0
	while stepStart < len(samples):
		stepEnd = stepStart + chunkSize
		if stepEnd < len(samples):
			yield i, samples[stepStart:stepEnd], labels[stepStart:stepEnd]
			i += 1
		stepStart = stepEnd


class Network():
	def __init__(self, num_hidden, batch_size, conv_depth, kernel_size, pooling_scale):

		self.batch_size = batch_size
		self.test_batch_size = 500

		# Hyper Parameters
		self.num_hidden = num_hidden
		self.kernel_size = kernel_size     # 卷积核的大小
		self.conv1_depth = conv_depth      # 卷积的深度(也就是卷积核的个数)
		self.conv2_depth = conv_depth
		self.conv3_depth = conv_depth
		self.conv4_depth = conv_depth
		self.last_conv_depth = self.conv4_depth
		self.pooling_scale = pooling_scale
		self.pooling_stride = self.pooling_scale  # Max Pooling Stride

		# Graph Related
		self.graph = tf.Graph()
		self.tf_train_samples = None
		self.tf_train_labels = None
		self.tf_test_samples = None
		self.tf_test_labels = None
		self.tf_test_prediction = None

		# 统计
		self.merged = None
		self.train_summaries = []
		self.test_summaries = []

		"""
			添加summary.FileWriter
		"""
		self.define_graph()
		gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.333)
		self.session = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options), graph=self.graph)
		self.writer = tf.summary.FileWriter('./board', self.graph)

	def define_graph(self):
		'''
			定义我的计算图谱
		'''
		with self.graph.as_default():
			# 这里只是定义图谱中的各种变量
			with tf.name_scope('inputs'):
				self.tf_train_samples = tf.placeholder(
					tf.float32, shape=(self.batch_size, image_size, image_size, num_channels), name='tf_train_samples'
				)
				self.tf_train_labels  = tf.placeholder(
					tf.float32, shape=(self.batch_size, num_labels
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值