【问题标题】:How are input tensors with different shapes fed to neural network?如何将不同形状的输入张量馈送到神经网络?
【发布时间】:2021-02-22 19:44:39
【问题描述】:

我正在关注this tutorial 使用 Keras 的 Policy Gradient, 并且无法完全弄清楚以下内容。

在以下情况下,具有不同形状的输入张量究竟是如何馈送到模型的?
图层既不是.concated 也不是.Added。

  • input1.shape = (4, 4)
  • input2.shape = (4,)
  • “输入”层有4个神经元,接受input1+input2作为4d向量??

代码摘录(经过修改使其更简单):

import tensorflow as tf
from tensorflow.keras import layers, models, optimizers
from tensorflow.keras import backend as K
import numpy as np

input = tf.keras.Input(shape=(4, ))
advantages = tf.keras.Input(shape=[1])
dense1 = layers.Dense(32, activation='relu')(input)
dense2 = layers.Dense(32, activation='relu')(dense1)
output = layers.Dense(2, activation='softmax')(dense2)

model = tf.keras.Model(inputs=[input, advantages], outputs=[output])

# *********************************
input1 = np.array(
[[ 4.52281174e-02,  4.31672811e-02, -4.57789579e-02,  4.35560472e-02],
 [ 4.60914630e-02, -1.51269339e-01, -4.49078369e-02,  3.21451106e-01],
 [ 4.30660763e-02,  4.44624011e-02, -3.84788148e-02,  1.49510297e-02],
 [ 4.39553243e-02, -1.50087194e-01, -3.81797942e-02,  2.95249428e-01]]
)

input2 = np.array(
[ 1.60063125,  1.47153674,  1.34113826,  1.20942261]
)


label = np.array(
[[1, 0],
 [0, 1],
 [1, 0],
 [0, 1]]
)

model.compile(optimizer=optimizers.Adam(lr=0.0005), loss="binary_crossentropy")
model.train_on_batch([input1, input2], label)

【问题讨论】:

  • 查找批次和样本。

标签: tensorflow keras neural-network


【解决方案1】:

如果您可能想弄清楚您刚刚构建的图形类型,使用model.summary()tf.keras.utils.plot_model() 方法进行调试会很有帮助:

tf.keras.utils.plot_model(model, to_file="test.png", show_shapes=True, show_layer_names=True, show_dtype=True)

这会告诉你你的 input_2 确实没有被使用。由于您还没有通过任何操作将它连接到主图,因此它没有与之关联的权重(图运行但右侧没有要更新的内容):

【讨论】:

    猜你喜欢
    • 2020-12-28
    • 1970-01-01
    • 2018-10-20
    • 1970-01-01
    • 1970-01-01
    • 2017-07-30
    • 2017-08-31
    • 2020-05-26
    • 1970-01-01
    相关资源
    最近更新 更多