在tensorflow层中.dense(输入,单位,激活)实现具有任意激活功能的多层感知器层。
输出=激活(matmul(输入,权重)+偏差)
通常,输入的形状为shape = [batch_size,input_size],可能看起来像这样:(单位= 128,激活= tf.nn.relu是任意选择的)
inputx = tf.placeholder(float, shape=[batch_size, input_size])
dense_layer = tf.layers.dense(inputx, 128, tf.nn.relu)
Run Code Online (Sandbox Code Playgroud)
我没有找到任何有关如何输入高维输入的文档,例如,因为可能输入time_steps导致张量为shape = [time_step,batch_size,input_size]。这里想要的是将层应用于批次的每个元素的每个时间步的每个单个input_vector。换个说法,layers.dense()的内部矩阵应该简单地以numpy样式使用广播。我在这里期望的行为实际发生了什么?即是:
inputx = tf.placeholder(float, shape=[time_step, batch_size, input_size])
dense_layer = tf.layers.dense(inputx, 128, tf.nn.relu)
Run Code Online (Sandbox Code Playgroud)
在batch_size中的每个元素的每个time_step上,将密集层应用于大小为input_size的每个输入?然后这应该导致张量(在上面的density_layer中)的形状为[time_step,batch_size,128]我在问,例如tf.matmul不支持numpy风格的广播,所以我不确定张量流如何处理这些情况。
您可以通过检查密集内核的形状来验证您的期望,如下所示。
>>> inputx = tf.placeholder(float, shape=[2,3,4])
>>> dense_layer = tf.layers.dense(inputx, 128, tf.nn.relu)
>>> g=tf.get_default_graph()
>>> g.get_collection('variables')
[<tf.Variable 'dense/kernel:0' shape=(4, 128) dtype=float32_ref>, <tf.Variable 'dense/bias:0' shape=(128,) dtype=float32_ref>]
Run Code Online (Sandbox Code Playgroud)
密集层的行为与卷积层相同。
您可以将 inputx 视为宽度=2、高度=3 和通道=4 的图像,而密集层作为具有 128 个滤波器的卷积层,滤波器大小为 1*1。
| 归档时间: |
|
| 查看次数: |
891 次 |
| 最近记录: |