python - 如何在 Keras 中将输入拆分为不同的 channel

标签 python tensorflow keras multiprocessing keras-layer

我有 20 个 channel 数据,每个 channel 有 5000 个值(总共 150,000 多条记录以 .npy 文件形式存储在 HD 上)。

我正在关注 https://stanford.edu/~shervine/blog/keras-how-to-generate-data-on-the-fly.html 上提供的 keras fit_generator 教程读取数据(每条记录被读取为 (5000, 20) float32 类型的 numpy 数组。

我已经理论化的网络,每个 channel 都有并行卷积网络,这些 channel 在末尾连接到,因此需要并行馈送数据。 从数据中仅读取和馈送单个 channel 并馈送到单个网络是成功的

def __data_generation(self, list_IDs_temp):
    'Generates data containing batch_size samples' # X : (n_samples, *dim, n_channels)
    # Initialization
    if(self.n_channels == 1):
        X = np.empty((self.batch_size, *self.dim))
    else:
        X = np.empty((self.batch_size, *self.dim, self.n_channels))
    y = np.empty((self.batch_size), dtype=int)

    # Generate data
    for i, ID in enumerate(list_IDs_temp):
        # Store sample
        d = np.load(self.data_path + ID + '.npy')
        d = d[:, self.required_channel]
        d = np.expand_dims(d, 2)
        X[i,] = d

        # Store class
        y[i] = self.labels[ID]

    return X, keras.utils.to_categorical(y, num_classes=self.n_classes)

然而,当读取整个记录并尝试通过使用 Lambda 层进行切片将其提供给网络时,我得到了

读取整条记录

 X[i,] = np.load(self.data_path + ID + '.npy')

使用可用的 Lambda 切片层实现:https://github.com/keras-team/keras/issues/890并打电话

input = Input(shape=(5000, 20))
slicedInput = crop(2, 0, 1)(input)

我能够编译模型并显示预期的层大小。

当数据被馈送到这个网络时,我得到

ValueError: could not broadcast input array from shape (5000,20) into shape (5000,1)

任何帮助将不胜感激....

最佳答案

如 Github 中所述 thread您正在引用,Lambda 层只能返回一个输出,因此建议 crop(dimension, start, end)仅返回一个“给定维度上从开始到结束的张量”。

我相信您可以通过这种方式实现您想要实现的目标:

from keras.layers import Dense, Concatenate, Input, Lambda
from keras.models import Model

num_channels = 20
input = Input(shape=(5000, num_channels))

branch_outputs = []
for i in range(num_channels):
    # Slicing the ith channel:
    out = Lambda(lambda x: x[:, i])(input)

    # Setting up your per-channel layers (replace with actual sub-models):
    out = Dense(16)(out)
    branch_outputs.append(out)

# Concatenating together the per-channel results:
out = Concatenate()(branch_outputs)

# Adding some further layers (replace or remove with your architecture):
out = Dense(10)(out)

# Building model:
model = Model(inputs=input, outputs=out)    
model.compile(optimizer=keras.optimizers.Adam(lr=0.001), loss='categorical_crossentropy', metrics=['accuracy'])

# --------------
# Generating dummy data:
import numpy as np
data = np.random.random((64, 5000, num_channels))
targets = np.random.randint(2, size=(64, 10))

# Training the model:
model.fit(data, targets, epochs=2, batch_size=32)
# Epoch 1/2
# 32/64 [==============>...............] - ETA: 1s - loss: 37.1219 - acc: 0.1562
# 64/64 [==============================] - 2s 27ms/step - loss: 38.4801 - acc: 0.1875
# Epoch 2/2
# 32/64 [==============>...............] - ETA: 0s - loss: 38.9541 - acc: 0.0938
# 64/64 [==============================] - 0s 4ms/step - loss: 36.0179 - acc: 0.1875

关于python - 如何在 Keras 中将输入拆分为不同的 channel ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50701913/

相关文章:

python - 将二维 numpy 数组转换为数据帧行

python - 在 Keras 中使用 'selu' 激活函数时出错

python - “MultiRNNCell”对象不可迭代 Python Tensorflow

python - 类型错误 : object of type 'InputLayer' has no len()

python - 如何在 Tensorflow 2.0 中获得其他指标(不仅仅是准确性)?

python-3.x - 从 Tensorflow Keras 检查点重新加载最佳权重

python - Scikit Learn - 交叉验证后对数据集进行评分

python - 正则表达式向后匹配任何内容,直到字符串第一次出现

python - 如何找到将项目移动到堆栈中某个位置的最小移动次数?

git - 对等错误 TensorFlow 重置连接