python concatenate fit_python-Keras fit_generator问题

我遵循this tutorial为我的Keras模型创建自定义生成器.这是显示我所面临问题的MWE:

import sys, keras

import numpy as np

import tensorflow as tf

import pandas as pd

from keras.models import Model

from keras.layers import Dense, Input

from keras.optimizers import Adam

from keras.losses import binary_crossentropy

class DataGenerator(keras.utils.Sequence):

'Generates data for Keras'

def __init__(self, list_IDs, batch_size, shuffle=False):

'Initialization'

self.batch_size = batch_size

self.list_IDs = list_IDs

self.shuffle = shuffle

self.on_epoch_end()

def __len__(self):

'Denotes the number of batches per epoch'

return int(np.floor(len(self.list_IDs) / self.batch_size))

def __getitem__(self, index):

'Generate one batch of data'

# Generate indexes of the batch

#print('self.batch_size: ', self.batch_size)

print('index: ', index)

sys.exit()

def on_epoch_end(self):

'Updates indexes after each epoch'

self.indexes = np.arange(len(self.list_IDs))

print('self.indexes: ', self.indexes)

if self.shuffle == True:

np.random.shuffle(self.indexes)

def __data_generation(self, list_IDs_temp):

'Generates data containing batch_size samples' # X : (n_samples, *dim, n_channels)

X1 = np.empty((self.batch_size, 10), dtype=float)

X2 = np.empty((self.batch_size, 12), dtype=int)

#Generate data

for i, ID in enumerate(list_IDs_temp):

print('i is: ', i, 'ID is: ', ID)

#Preprocess this sample (omitted)

X1[i,] = np.repeat(1, X1.shape[1])

X2[i,] = np.repeat(2, X2.shape[1])

Y = X1[:,:-1]

return X1, X2, Y

if __name__=='__main__':

train_ids_to_use = list(np.arange(1, 321)) #1, 2, ...,320

valid_ids_to_use = list(np.arange(321, 481)) #321, 322, ..., 480

params = {'batch_size': 32}

train_generator = DataGenerator(train_ids_to_use, **params)

valid_generator = DataGenerator(valid_ids_to_use, **params)

#Build a toy model

input_1 = Input(shape=(3, 10))

input_2 = Input(shape=(3, 12))

y_input = Input(shape=(3, 10))

concat_1 = keras.layers.concatenate([input_1, input_2])

concat_2 = keras.layers.concatenate([concat_1, y_input])

dense_1 = Dense(10, activation='relu')(concat_2)

output_1 = Dense(10, activation='sigmoid')(dense_1)

model = Model([input_1, input_2, y_input], output_1)

print(model.summary())

#Compile and fit_generator

model.compile(optimizer=Adam(lr=0.001), loss=binary_crossentropy)

model.fit_generator(generator=train_generator, validation_data = valid_generator, epochs=2, verbose=2)

我不想打乱我的输入数据.我以为这已经得到解决,但是在我的代码中,当我在__get_item__中打印索引时,我得到了随机数.我想要连续的数字.注意,我正在尝试使用__getitem__中的sys.exit终止进程,以查看发生了什么事情.

我的问题:

>为什么索引不能连续显示?我怎样才能解决这个问题?

>当我在使用屏幕的终端中运行此命令时,为什么它不响应Ctrl C?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值