无法使用numpy数组训练模型

时间:2018-10-09 08:48:26

标签: python numpy neural-network deep-learning autoencoder

我正在训练以下有关浮点数的自动编码器。

input_img = Input(shape=(2623,1), name='input')

x = ZeroPadding1D(1)(input_img)
x = Conv1D(32, 3, activation='relu', padding='same', use_bias=False)(input_img)
x = BatchNormalization(axis=-1)(x)
x = MaxPooling1D(2, padding='same')(x)
x = Conv1D(16, 3, activation='relu', padding='same', use_bias=False)(x)
x = BatchNormalization(axis=-1)(x)
x = MaxPooling1D(2, padding='same')(x)
x = Conv1D(16,3, activation='relu', padding='same', use_bias=False)(x)
x = BatchNormalization(axis=-1)(x)
encoded = MaxPooling1D(2, padding='same')(x)


x = Conv1D(16,3, activation='relu', padding='same', use_bias=False)(encoded)
x = BatchNormalization(axis=-1)(x)
x = UpSampling1D(2)(x)
x = Conv1D(16,3, activation='relu', padding='same', use_bias=False)(x)
x = BatchNormalization(axis=-1)(x)
x = UpSampling1D(2)(x)
x = Conv1D(32, 3, activation='relu', padding='same', use_bias=False)(x)  #input_shape=(30, 1))
x = BatchNormalization(axis=-1)(x)  
x = UpSampling1D(2)(x)
x = Cropping1D(cropping=(0, 1))(x) #Crop nothing from input but crop 1 element from the end
decoded = Conv1D(1, 3, activation='sigmoid', padding='same', use_bias=False)(x)  

autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='rmsprop', loss='binary_crossentropy')


x = Input(shape=(16, 300), name="input")
h = x
h = Conv1D(filters=300, kernel_size=16,
             activation="relu", padding='same', name='Conv1')(h)
h = MaxPooling1D(pool_size=16, name='Maxpool1')(h)

我必须将数据转换为numpy数组才能进行处理,但是当模型开始训练时,我得到了:

  

ValueError:无法将字符串转换为浮点数:

之所以发生这种情况,是因为我的训练数据如下:

  

train [0] [1]#一份训练数据

array(['0.001758873'], dtype=object)

我应该怎么做才能避免训练数据中出现“ dtype = object”,或者我是否必须将其转换为其他东西? 谢谢!

1 个答案:

答案 0 :(得分:1)

也许,作为预处理步骤,您可以使用以下方法将对象类型数组转换为浮点数组:

# if float32 is the desired & appropriate datatype
train = train.astype(numpy.float32)