如何将 numpy.ndarray 或 keras metrix 作为 keras 输入传递

How to pass a numpy.ndarray or keras metrix as a keras input

我有一个形状为 (2,30000) 的矩阵列表,我需要使用转换层将此信息作为带有张量流的深度学习模型的输入传递,但是当我尝试传递此信息以训练所有我获得的时间

    return ops.EagerTensor(value, ctx.device_name, dtype)
ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray).

我有一个 pandas 系列的 numpy 数组列表

0      [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,...
1      [[0.036600337822677166, 0.018300168911338583, ...
2      [[0.8671148170073495, 0.47065322955211747, 0.0...
3      [[0.024680190751413082, 0.007051483071832309, ...
4      [[0.0688791198957804, 0.0, 0.0, 0.0, 0.0229597...
                             ...                        
549    [[0.024182541670333724, 0.0, 0.0, 0.0, 0.0, 0....
550    [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,...

Name: tfidf, Length: 554, dtype: object

这是我的模型

def getmodel(num_words = 20000):
    x_input = keras.Input( shape=(2,30000),name="article1")  # Variable-length sequence of ints
    conv1d_1= layers.Conv1D(64, 2, input_shape=(2,30000), activation='relu')(x_input)
    global_1 = layers.GlobalMaxPooling1D()(conv1d_1)
    dense1 = layers.Dense(1024, name="dense1", activation="relu", )(global_1)
    encoder_conv_dense2 = layers.Dense(256, name="40_dense", activation="relu", )(dense1)
    encoder_conv_dense3 = layers.Dense(1, name="similar_result", activation="relu", )(encoder_conv_dense2)

    model = keras.Model(
        inputs=x_input,
        outputs=[encoder_conv_dense3],
    )
    keras.utils.plot_model(model, "my_paper_model.png", show_shapes=True)

    return model

这是我如何尝试传递信息

def compile_model(model, optimizer, loss, loss_weight):
    model.compile(
        optimizer=optimizer,
        loss=loss,
        loss_weights=loss_weight,
        metrics=[tf.keras.metrics.Accuracy()]
    )
    return model

def train(df):
callback = tf.keras.callbacks.EarlyStopping(monitor='accuracy', patience=40)
    callback2 = tf.keras.callbacks.EarlyStopping(monitor='loss', patience=20)
    model = getmodel()
    model = compile_model(model, keras.optimizers.Adam(), keras.losses.BinaryCrossentropy(), 0.05)
    history = model.fit(
        {"article1":df['tfidf']},
        {"similar_result": df['is_similar']},
        validation_split=0.3,
        epochs=400,
        batch_size=32,
        verbose=1,
        callbacks=[callback, callback2, ],
    )

我不知道如何将我的 2 行 30,000 列的 numpy 矩阵作为 keras 的输入并将其与转换层一起使用。

希望有人能帮助我

您提供的输入格式不正确。在这种情况下,对于 3D 输入,您需要提供一个形状数组(n_sample、时间步长、n_features)。您可以使用 np.stack(df['tfidf'],0) 简单地实现此目的,这将导致形状为 (n_sample, 1, 30000).

的数组

我尝试在下面重现一个虚拟示例:

# create fake data
df = pd.DataFrame()
df['tfidf'] = [[np.random.uniform(0,1, 30)],
              [np.random.uniform(0,1, 30)],
              [np.random.uniform(0,1, 30)],
              [np.random.uniform(0,1, 30)]]
df['is_similar'] = np.random.randint(0,2, 4)

df['tfidf'] 的格式是:

0    [[0.09865182564241004, 0.5282608042987893, 0.5...
1    [[0.1361578046476558, 0.9866056058771036, 0.44...
2    [[0.38811373040427766, 0.5686225139326878, 0.8...
3    [[0.8254123154336716, 0.3542711784901068, 0.28...
Name: tfidf, dtype: object

定义模型并拟合:

def getmodel():
    
    x_input = keras.Input(shape=(1,30),name="article1")  # Variable-length sequence of ints
    conv1d_1= layers.Conv1D(64, 1, activation='relu')(x_input)
    flat = layers.Flatten()(conv1d_1)
    dense1 = layers.Dense(1024, name="dense1", activation="relu")(flat)
    encoder_conv_dense2 = layers.Dense(256, name="40_dense", activation="relu")(dense1)
    encoder_conv_dense3 = layers.Dense(1, name="similar_result", activation="sigmoid")(encoder_conv_dense2)

    model = keras.Model(
        inputs=x_input,
        outputs=encoder_conv_dense3,
    )
    
    return model

model = getmodel()
model.compile('adam', 'binary_crossentropy')
history = model.fit(
    {"article1": np.stack(df['tfidf'],0)},
    {"similar_result": df['is_similar']},
    epochs=3,
)