tensorflow DCGAN 源码中 conv_cond_concat函数

源码地址:

https://github.com/carpedm20/DCGAN-tensorflow


今天看了源码中,再debug调试时看到了conv_cond_concat这个函数,第一反应就是应该与tf.concat有关系,看了源码确实是。

以下是函数定义:

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)

这里面有

 y*tf.ones([x_shapes[0], x_shapes[1], x_shapes[2], y_shapes[3]])]
得注意一下,这个是tensorflow中的乘法表示,tf.ones是全1的矩阵。

这个单独测试一下,看效果比较直观:

#!/usr/bin/env python
#coding:utf8

import os,sys

import numpy as np
import tensorflow as tf

y=tf.ones([1,1,3])
y=np.array([1,2,3])
z=y*tf.ones([4,4,3])
with tf.Session():
    print(z.eval())
结果为:

4个4*3的矩阵,矩阵每一行都是一个y。

[[[ 1.  2.  3.]
  [ 1.  2.  3.]
  [ 1.  2.  3.]
  [ 1.  2.  3.]]

 [[ 1.  2.  3.]
  [ 1.  2.  3.]
  [ 1.  2.  3.]
  [ 1.  2.  3.]]

 [[ 1.  2.  3.]
  [ 1.  2.  3.]
  [ 1.  2.  3.]
  [ 1.  2.  3.]]

 [[ 1.  2.  3.]
  [ 1.  2.  3.]
  [ 1.  2.  3.]
  [ 1.  2.  3.]]]

Process finished with exit code 0
所以,不难看出,这就是一个相当于一个复制粘贴改变大小的操作。

最后concat很简单就是将两个矩阵(只有要连接起来的那一维不一样,其他维度都一样的矩阵)连接起来。这里是将最后一维连接起来。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值