- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这是我的问题,我想在 TimeDistributed 层中使用预训练 CNN 网络之一。但是我在实现它时遇到了一些问题。
这是我的模型:
def bnn_model(max_len):
# sequence length and resnet input size
x = Input(shape=(maxlen, 224, 224, 3))
base_model = ResNet50.ResNet50(weights='imagenet', include_top=False)
for layer in base_model.layers:
layer.trainable = False
som = TimeDistributed(base_model)(x)
#the ouput of the model is [1, 1, 2048], need to squeeze
som = Lambda(lambda x: K.squeeze(K.squeeze(x,2),2))(som)
bnn = Bidirectional(LSTM(300))(som)
bnn = Dropout(0.5)(bnn)
pred = Dense(1, activation='sigmoid')(bnn)
model = Model(input=x, output=pred)
model.compile(optimizer=Adam(lr=1.0e-5), loss="mse", metrics=["accuracy"])
return model
编译模型时没有错误。但是当我开始训练时,出现以下错误:
tensorflow/core/framework/op_kernel.cc:975] Invalid argument: You must feed a value for placeholder tensor 'input_2' with dtype float
[[Node: input_2 = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/gpu:0"]()]]
我检查过并确实发送了 float32,但对于 input1,input2 是预训练 Resnet 中存在的输入。
这里只是为了概览一下模型摘要。 (注意:很奇怪,它没有显示 Resnet 内部发生了什么但没关系)
____________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
====================================================================================================
input_1 (InputLayer) (None, 179, 224, 224, 0
____________________________________________________________________________________________________
timedistributed_1 (TimeDistribut (None, 179, 1, 1, 204 23587712 input_1[0][0]
____________________________________________________________________________________________________
lambda_1 (Lambda) (None, 179, 2048) 0 timedistributed_1[0][0]
____________________________________________________________________________________________________
bidirectional_1 (Bidirectional) (None, 600) 5637600 lambda_1[0][0]
____________________________________________________________________________________________________
dropout_1 (Dropout) (None, 600) 0 bidirectional_1[0][0]
____________________________________________________________________________________________________
dense_1 (Dense) (None, 1) 601 dropout_1[0][0]
====================================================================================================
Total params: 29,225,913
Trainable params: 5,638,201
Non-trainable params: 23,587,712
____________________________________________________________________________________________________
我猜我没有正确使用 TimeDistributed,而且我看到没有人尝试这样做。我希望有人可以指导我。
编辑:
问题来自 ResNet50.ResNet50(weights='imagenet', include_top=False)
在图中创建自己的输入。
所以我想我需要做一些像 ResNet50.ResNet50(weights='imagenet', input_tensor=x, include_top=False)
但我不知道如何将它与 结合时间分布
。
我试过了
base_model = Lambda(lambda x : ResNet50.ResNet50(weights='imagenet', input_tensor=x, include_top=False))
som = TimeDistributed(base_model)(in_ten)
但它不起作用。
最佳答案
我的简单解决方案很漂亮。
考虑到您使用的是来自 keras 的预训练网络,您也可以将其替换为您自己的预训练网络。
这是一个简单的解决方案::
model_vgg=keras.applications.VGG16(input_shape=(256, 256, 3),
include_top=False,
weights='imagenet')
model_vgg.trainable = False
model_vgg.summary()
如果你想使用任何中间层,否则将'block2_pool'替换为最后一层的名称::
intermediate_model= Model(inputs=model_vgg.input, outputs=model_vgg.get_layer('block2_pool').output)
intermediate_model.summary()
最后将它包裹在一个 TimeDistributed 层中
input_tensor = Input(shape=(time_steps,height, width, channels))
timeDistributed_layer = TimeDistributed( intermediate_model )(input_tensor)
现在你可以简单地做::
my_time_model = Model( inputs = input_tensor, outputs = timeDistributed_layer )
关于python - Keras 使用 TimeDistributed 预训练 CNN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42313412/
我有兴趣在 tf.keras 中训练一个模型,然后用 keras 加载它。我知道这不是高度建议,但我对使用 tf.keras 来训练模型很感兴趣,因为 tf.keras 更容易构建输入管道 我想利用
我进行了大量搜索,但仍然无法弄清楚如何编写具有多个交互输出的自定义损失函数。 我有一个神经网络定义为: def NeuralNetwork(): inLayer = Input((2,));
我正在阅读一篇名为 Differential Learning Rates 的文章在 Medium 上,想知道这是否可以应用于 Keras。我能够找到在 pytorch 中实现的这项技术。这可以在 K
我正在实现一个神经网络分类器,以打印我正在使用的这个神经网络的损失和准确性: score = model.evaluate(x_test, y_test, verbose=False) model.m
我最近在查看模型摘要时遇到了这个问题。 我想知道,[(None, 16)] 和有什么区别?和 (None, 16) ?为什么输入层有这样的输入形状? 来源:model.summary() can't
我正在尝试使用 Keras 创建自定义损失函数。我想根据输入计算损失函数并预测神经网络的输出。 我尝试在 Keras 中使用 customloss 函数。我认为 y_true 是我们为训练提供的输出,
我有一组样本,每个样本都是一组属性的序列(例如,一个样本可以包含 10 个序列,每个序列具有 5 个属性)。属性的数量总是固定的,但序列的数量(时间戳)可能因样本而异。我想使用这个样本集在 Keras
Keras 在训练集和测试集文件夹中发现了错误数量的类。我有 3 节课,但它一直说有 4 节课。有人可以帮我吗? 这里的代码: cnn = Sequential() cnn.add(Conv2D(32
我想编写一个自定义层,在其中我可以在两次运行之间将变量保存在内存中。例如, class MyLayer(Layer): def __init__(self, out_dim = 51, **kwarg
我添加了一个回调来降低学习速度: keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=100,
在 https://keras.io/layers/recurrent/我看到 LSTM 层有一个 kernel和一个 recurrent_kernel .它们的含义是什么?根据我的理解,我们需要 L
问题与标题相同。 我不想打开 Python,而是使用 MacOS 或 Ubuntu。 最佳答案 Python 库作者将版本号放入 .__version__ 。您可以通过在命令行上运行以下命令来打印它:
Keras 文档并不清楚这实际上是什么。我知道我们可以用它来将输入特征空间压缩成更小的空间。但从神经设计的角度来看,这是如何完成的呢?它是一个自动编码器,RBM吗? 最佳答案 据我所知,嵌入层是一个简
我想实现[http://ydwen.github.io/papers/WenECCV16.pdf]中解释的中心损失]在喀拉斯 我开始创建一个具有 2 个输出的网络,例如: inputs = Input
我正在尝试实现多对一模型,其中输入是大小为 的词向量d .我需要输出一个大小为 的向量d 在 LSTM 结束时。 在此 question ,提到使用(对于多对一模型) model = Sequenti
我有不平衡的训练数据集,这就是我构建自定义加权分类交叉熵损失函数的原因。但问题是我的验证集是平衡的,我想使用常规的分类交叉熵损失。那么我可以在 Keras 中为验证集传递不同的损失函数吗?我的意思是用
DL 中的一项常见任务是将输入样本归一化为零均值和单位方差。可以使用如下代码“手动”执行规范化: mean = np.mean(X, axis = 0) std = np.std(X, axis =
我正在尝试学习 Keras 并使用 LSTM 解决分类问题。我希望能够绘制 准确率和损失,并在训练期间更新图。为此,我正在使用 callback function . 由于某种原因,我在回调中收到的准
在 Keras 内置函数中嵌入使用哪种算法?Word2vec?手套?其他? https://keras.io/layers/embeddings/ 最佳答案 简短的回答是都不是。本质上,GloVe 的
我有一个使用 Keras 完全实现的 LSTM RNN,我想使用梯度剪裁,梯度范数限制为 5(我正在尝试复制一篇研究论文)。在实现神经网络方面,我是一个初学者,我将如何实现? 是否只是(我正在使用 r
我是一名优秀的程序员,十分优秀!