我在Keras中有以下代码(基本上我正在修改此代码以供我使用)并且我收到此错误:
'ValueError:检查目标时出错:预期conv3d_3有5个维度,但得到的数组有形状(10,4096)'
码:
from keras.models import Sequential
from keras.layers.convolutional import Conv3D
from keras.layers.convolutional_recurrent import ConvLSTM2D
from keras.layers.normalization import BatchNormalization
import numpy as np
import pylab as plt
from keras import layers
# We create a layer which take as input movies of shape
# (n_frames, width, height, channels) and returns a movie
# of identical shape.
model = Sequential()
model.add(ConvLSTM2D(filters=40, kernel_size=(3, 3),
input_shape=(None, 64, 64, 1),
padding='same', return_sequences=True))
model.add(BatchNormalization())
model.add(ConvLSTM2D(filters=40, kernel_size=(3, 3),
padding='same', return_sequences=True))
model.add(BatchNormalization())
model.add(ConvLSTM2D(filters=40, kernel_size=(3, 3),
padding='same', return_sequences=True)) …Run Code Online (Sandbox Code Playgroud) 我有一个用Keras编码的神经网络模型.
当我在笔记本电脑上运行它时,我有以下输出显示模型的进度:
训练4个样本,验证1个样本Epoch 1/1 4/4 [==============================] - 22s 5s /步 - 损失:0.2477 - val_loss:0.2672
但是,当我将此代码提交给集群进行运行时,我不知道剩下多少个时期,所以我想在模型运行时将上述输出保存到文件中.
我怎样才能做到这一点?
我见过在 Keras 中使用 LSTM 构建编码器-解码器网络的示例,但我想要一个 ConvLSTM 编码器-解码器,并且由于 ConvLSTM2D 不接受任何“initial_state”参数,因此我可以将编码器的初始状态传递给解码器,我尝试在 Keras 中使用 RNN 并尝试将 ConvLSTM2D 作为 RNN 的单元传递,但出现以下错误:
ValueError: ('`cell` should have a `call` method. The RNN was passed:', <tf.Tensor 'encoder_1/TensorArrayReadV3:0' shape=(?, 62, 62, 32) dtype=float32>)
Run Code Online (Sandbox Code Playgroud)
这就是我尝试定义 RNN 单元的方式:
first_input = Input(shape=(None, 62, 62, 12))
encoder_convlstm2d = ConvLSTM2D(filters=32, kernel_size=(3, 3),
padding='same',
name='encoder'+ str(1))(first_input )
encoder_outputs, state_h, state_c = keras.layers.RNN(cell=encoder_convlstm2d, return_sequences=False, return_state=True, go_backwards=False,
stateful=False, unroll=False)
Run Code Online (Sandbox Code Playgroud) conv-neural-network keras tensorflow recurrent-neural-network encoder-decoder
我正在尝试使用 BERT 进行文档嵌入。我使用的代码是两个来源的组合。我使用BERT Document Classification Tutorial with Code和BERT Word Embeddings Tutorial。下面是代码,我将每个文档的前 510 个令牌提供给 BERT 模型。最后,我将 K 均值聚类应用于这些嵌入,但每个聚类的成员完全无关。我想知道这怎么可能。也许我的代码有问题。如果您查看我的代码并判断它是否有问题,我将不胜感激。我使用 Google colab 来运行此代码。
# text_to_embedding function
import torch
from keras.preprocessing.sequence import pad_sequences
def text_to_embedding(tokenizer, model, in_text):
'''
Uses the provided BERT 'model' and 'tokenizer' to generate a vector
representation of the input string, 'in_text'.
Returns the vector stored as a numpy ndarray.
'''
# ===========================
# STEP 1: Tokenization
# ===========================
MAX_LEN = 510
# 'encode' will:
# (1) …Run Code Online (Sandbox Code Playgroud)