生成对抗网络DCGAN+Tensorflow代码学习笔记(三)----ops.py
来源:互联网 发布:java 游戏引擎 编辑:程序博客网 时间:2024/05/17 06:33
ops.py主要定义了一些变量连接的函数、批处理规范化的函数、卷积函数、解卷积函数、激励函数、线性运算函数。
import mathimport numpy as np import tensorflow as tf#导入tensorflow.python.framework模块,包含了tensorflow中图、张量等的定义操作from tensorflow.python.framework import opsfrom utils import *try: image_summary = tf.image_summary scalar_summary = tf.scalar_summary histogram_summary = tf.histogram_summary merge_summary = tf.merge_summary SummaryWriter = tf.train.SummaryWriterexcept: image_summary = tf.summary.image scalar_summary = tf.summary.scalar histogram_summary = tf.summary.histogram merge_summary = tf.summary.merge SummaryWriter = tf.summary.FileWriter#连接多个tensorif "concat_v2" in dir(tf): def concat(tensors, axis, *args, **kwargs): return tf.concat_v2(tensors, axis, *args, **kwargs)else: def concat(tensors, axis, *args, **kwargs): return tf.concat(tensors, axis, *args, **kwargs)#函数批处理规范化class batch_norm(object): def __init__(self, epsilon=1e-5, momentum = 0.9, name="batch_norm"): with tf.variable_scope(name): self.epsilon = epsilon self.momentum = momentum self.name = name def __call__(self, x, train=True): return tf.contrib.layers.batch_norm(x, decay=self.momentum, updates_collections=None, epsilon=self.epsilon, scale=True, is_training=train, scope=self.name)#定义conv_cond_concat(x,y)函数。连接x,y与Int32型的[x_shapes[0], x_shapes[1], x_shapes[2], y_shapes[3]]维度的张量乘积。def conv_cond_concat(x, y): """Concatenate conditioning vector on feature map axis.""" x_shapes = x.get_shape() y_shapes = y.get_shape() return concat([ x, y*tf.ones([x_shapes[0], x_shapes[1], x_shapes[2], y_shapes[3]])], 3)#卷积层def conv2d(input_, output_dim, k_h=5, k_w=5, d_h=2, d_w=2, stddev=0.02, name="conv2d"): with tf.variable_scope(name): w = tf.get_variable('w', [k_h, k_w, input_.get_shape()[-1], output_dim], initializer=tf.truncated_normal_initializer(stddev=stddev)) conv = tf.nn.conv2d(input_, w, strides=[1, d_h, d_w, 1], padding='SAME') biases = tf.get_variable('biases', [output_dim], initializer=tf.constant_initializer(0.0)) conv = tf.reshape(tf.nn.bias_add(conv, biases), conv.get_shape()) return conv#反卷积层def deconv2d(input_, output_shape, k_h=5, k_w=5, d_h=2, d_w=2, stddev=0.02, name="deconv2d", with_w=False): with tf.variable_scope(name): # filter : [height, width, output_channels, in_channels] w = tf.get_variable('w', [k_h, k_w, output_shape[-1], input_.get_shape()[-1]], initializer=tf.random_normal_initializer(stddev=stddev)) try: deconv = tf.nn.conv2d_transpose(input_, w, output_shape=output_shape, strides=[1, d_h, d_w, 1]) # Support for verisons of TensorFlow before 0.7.0 except AttributeError: deconv = tf.nn.deconv2d(input_, w, output_shape=output_shape, strides=[1, d_h, d_w, 1]) biases = tf.get_variable('biases', [output_shape[-1]], initializer=tf.constant_initializer(0.0)) deconv = tf.reshape(tf.nn.bias_add(deconv, biases), deconv.get_shape()) if with_w: return deconv, w, biases else: return deconv#定义一个lrelu激励函数def lrelu(x, leak=0.2, name="lrelu"): return tf.maximum(x, leak*x)#进行线性运算,获取一个随机正态分布矩阵,获取初始偏置值,如果with_w为真,则返回xw+b,权值w和偏置值b;否则返回xw+b。def linear(input_, output_size, scope=None, stddev=0.02, bias_start=0.0, with_w=False): shape = input_.get_shape().as_list() with tf.variable_scope(scope or "Linear"): matrix = tf.get_variable("Matrix", [shape[1], output_size], tf.float32, tf.random_normal_initializer(stddev=stddev)) bias = tf.get_variable("bias", [output_size], initializer=tf.constant_initializer(bias_start)) if with_w: return tf.matmul(input_, matrix) + bias, matrix, bias else: return tf.matmul(input_, matrix) + bias
阅读全文
0 0
- 生成对抗网络DCGAN+Tensorflow代码学习笔记(三)----ops.py
- 生成对抗网络DCGAN+Tensorflow代码学习笔记(一)----main.py
- 生成对抗网络DCGAN+Tensorflow代码学习笔记(二)----utils.py
- TensorFlow/对抗网络DCGAN生成图片
- 生成对抗网络学习笔记5----DCGAN(unsupervised representation learning with deep convolutional generative adv)的实现
- 生成对抗网络学习笔记5----DCGAN(unsupervised representation learning with deep convolutional generative adv)的实现
- 深度卷积对抗生成网络(DCGAN)
- 深度卷积对抗生成网络(DCGAN)
- 深度卷积对抗生成网络(DCGAN)
- 深度卷积对抗生成网络(DCGAN)
- 深度卷积生成对抗网络--DCGAN
- 《白话深度学习与Tensorflow》学习笔记(6)生成式对抗网络(GAN)
- 生成对抗网络的简单介绍(TensorFlow 代码)
- 生成对抗网络简介(包含TensorFlow代码示例)【翻译】
- 生成对抗网络(GAN)原理+tensorflow代码实现
- 生成对抗网络介绍(附TensorFlow代码)
- 学习笔记-对抗生成网络
- 【神经网络与深度学习】生成式对抗网络GAN研究进展(五)——Deep Convolutional Generative Adversarial Nerworks,DCGAN
- 【JavaScript学习】DOM:Attr类型
- “/”应用程序中的服务器错误
- LeetCode-Climbing Stairs(爬楼梯问题)
- leetcode 290. Word Pattern
- 12.开源项目--git提交查找
- 生成对抗网络DCGAN+Tensorflow代码学习笔记(三)----ops.py
- 友盟第三方登录获取QQ的头像和昵称
- oracle密码到期(password expired)
- PullToRefresh
- ios-清除文件缓存
- 排序+删除+全选+反选+隔行变色
- 常见字符串函数的模拟实现(二)(c语言)
- Python踩坑之路-Python-3.6 安装pycrypto 2.6.1各种疑难杂症及解决方案
- 最小二乘回归