将 numpy ndarray 作为 keras 输入传递

Passing numpy ndarray as keras input

我有一个由 numpy 数组组成的数据集,我需要将此数据作为 keras 的输入传递。

Idx Target RF DL
0 P219109 [0.05555555555555555, 0.0, 0.0, 0.0, 0.0, 0.0,... [1.1074159, -5.242936, -6.9121795, 0.931392, -...
1 P219109 [0.5833333333333334, 0.0, 0.0, 0.0, 0.0, 0.0, ... [-9.173296, -4.847732, -2.5727227, 8.794523, 7...
2 P219109 [0.05555555555555555, 0.0, 0.0, 0.0, 0.0, 0.0,... [2.5204952, 1.3955389, -4.755222, -1.7222288, ...
3 P219109 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ... [1.4951401, 1.2499368, -3.08991, -2.0176327, -...
4 P219109 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ... [-3.4984744, 7.1746902, -0.36313212, -3.760725...

这是我的代码:

X = df[['RF', 'DL']]
y = df['Target']
_, y_ = np.unique(y, return_inverse=True)
y = y_
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, stratify=y, random_state=42)
train_labels = to_categorical(y_train)
test_labels = to_categorical(y_test)

model = models.Sequential()
model.add(layers.Dense(100, activation='relu', input_shape=(2,)))
model.add(layers.Dense(50, activation='relu'))
model.add(layers.Dense(40, activation='softmax'))
model.compile(optimizer='rmsprop',
                loss='categorical_crossentropy',
                metrics=['accuracy'])

model.fit(X_train, train_labels, epochs=10, batch_size=36)

我收到以下错误:

Traceback (most recent call last):
  File "d:/FINAL/main.py", line 81, in <module>
    model.fit(X_train, train_labels, epochs=20, batch_size=40)
  File "C:\Users\ni\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1050, in fit
    data_handler = data_adapter.DataHandler(
  File "C:\Users\ni\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 1100, in __init__
    self._adapter = adapter_cls(
  File "C:\Users\ni\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 263, in __init__
    x, y, sample_weights = _process_tensorlike((x, y, sample_weights))
  File "C:\Users\ni\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 1016, in _process_tensorlike
    inputs = nest.map_structure(_convert_numpy_and_scipy, inputs)
  File "C:\Users\ni\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\util\nest.py", line 659, in map_structure
    structure[0], [func(*x) for x in entries],
  File "C:\Users\ni\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\util\nest.py", line 659, in <listcomp>
    structure[0], [func(*x) for x in entries],
  File "C:\Users\ni\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 1011, in _convert_numpy_and_scipy
    return ops.convert_to_tensor_v2_with_dispatch(x, dtype=dtype)
  File "C:\Users\ni\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\util\dispatch.py", line 201, in wrapper
    return target(*args, **kwargs)
  File "C:\Users\ni\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\ops.py", line 1404, 
in convert_to_tensor_v2_with_dispatch
    return convert_to_tensor_v2(
  File "C:\Users\ni\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\ops.py", line 1410, 
in convert_to_tensor_v2
    return convert_to_tensor(
  File "C:\Users\ni\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\profiler\trace.py", line 163, 
in wrapped
    return func(*args, **kwargs)
  File "C:\Users\ni\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\ops.py", line 1540, 
in convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "C:\Users\ni\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\tensor_conversion_registry.py", line 52, in _default_conversion_function
    return constant_op.constant(value, dtype, name=name)
  File "C:\Users\ni\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\constant_op.py", line 264, in constant
    return _constant_impl(value, dtype, shape, name, verify_shape=False,
  File "C:\Users\ni\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\constant_op.py", line 276, in _constant_impl
    return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
  File "C:\Users\ni\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\constant_op.py", line 301, in _constant_eager_impl
    t = convert_to_eager_tensor(value, ctx, dtype)
  File "C:\Users\ni\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\framework\constant_op.py", line 98, in convert_to_eager_tensor
    return ops.EagerTensor(value, ctx.device_name, dtype)
ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray).

有人可以帮我解决这个问题吗?

凯拉斯:2.4.0 张量流:2.4.0 Python: 3.8

编辑: 如何将数据框对象作为 NumPy 数组传递? RF 列和 DL 列是 predict_proba 来自两个不同模型的结果。因此,我使用以下代码将其加入 pandas 数据框:

rf = [item for item in rf1]
dl = [item for item in out]
y = y_test
df = pd.Series(rf)
df = df.to_frame()
df.rename(columns = {0:'RF'}, inplace = True)
df['Target'] = y
df['DL'] = dl
cols = ['Target', 'RF', 'DL']
df = df[cols]

我也试过这个:

X = np.asarray(X).astype(np.float32)

但它不起作用,因为我收到以下错误:

ValueError: setting an array element with a sequence.

如评论中所述,最好直接创建数组而不是在中间使用 DataFrame。

问题是,即使 X 是一个 numpy 数组,它也包含其他数组,因为 Pandas returns 每行和每个单元格的数组。一个例子:

import pandas as pd
import numpy as np

df = pd.DataFrame({'RF':[np.array([1, 2, 3, 4, 5]), np.array([6, 2, 3, 4, 5])], 'DL': [np.array([1, 2, 3, 4, 5]), np.array([7, 2, 3, 4, 5])]})

print(df)
                RF               DL
0  [1, 2, 3, 4, 5]  [1, 2, 3, 4, 5]
1  [6, 2, 3, 4, 5]  [7, 2, 3, 4, 5]
X = df[['RF', 'DL']].to_numpy()

print(X)
[[array([1, 2, 3, 4, 5]) array([1, 2, 3, 4, 5])]
 [array([6, 2, 3, 4, 5]) array([7, 2, 3, 4, 5])]]

您可以重塑以尝试解决问题。这应该有效:

# You have to reshape each cell in each row.
X = np.array([np.reshape(x, (1, len(X[0][0]))) 
              for i in range(len(X)) 
              for x in X[i]]).reshape(-1, 2*len(X[0][0])).astype(np.float32)

print(X)
print(X.shape)
[[1. 2. 3. 4. 5. 1. 2. 3. 4. 5.]
 [6. 2. 3. 4. 5. 7. 2. 3. 4. 5.]]
(2, 10)

显然,这假设 'RF''DL' 列中的数组形状相同,我相信这是真的,因为它们是 predict_proba 的输出。