python - TypeError:float() 参数必须是字符串或数字,而不是 'builtin_function_or_method'
问题描述
我正在运行一个检查图像但不分类的 CNN。事实上,输出层是一个密集层,其参数是 1d 标签中图像的大小。
如下代码所示,我使用的是 model.fit_generator() 而不是 model.fit,当开始训练模型时出现以下错误:
TypeError:float() 参数必须是字符串或数字,而不是 'builtin_function_or_method'
我真的不明白为什么会这样。这里附上模型的摘要:
层(类型)输出形状参数#
conv2d_4 (Conv2D) (无, 26, 877, 32) 544
activation_5(激活)(无、26、877、32)0
max_pooling2d_4 (MaxPooling2 (无, 13, 438, 32) 0
conv2d_5 (Conv2D) (无, 12, 437, 16) 2064
activation_6(激活)(无、12、437、16)0
max_pooling2d_5 (MaxPooling2 (无, 6, 218, 16) 0
conv2d_6 (Conv2D) (无, 5, 217, 8) 520
activation_7(激活)(无、5、217、8)0
max_pooling2d_6 (MaxPooling2 (无, 2, 108, 8) 0
activation_8 (激活) (无, 2, 108, 8) 0
flatten_2(展平)(无,1728)0
dropout_2(辍学)(无,1728)0
dense_2(密集)(无,19316)33397364
==================================================== ================
总参数:33,400,492 可训练参数:33,400,492 不可训练参数:0
有什么建议么 ?提前非常感谢!
我已经查找了许多在线论坛/网站,但似乎没有找到适合我的情况。
def generator(data_arr, batch_size = 10):
num = len(data_arr)
if num % batch_size != 0 :
num = int(num/batch_size)
# Loop forever so the generator never terminates
while True:
for offset in range(0, num, batch_size):
batch_samples = (data_arr[offset:offset+batch_size])
samples = []
labels = []
for batch_sample in batch_samples:
samples.append(batch_sample[0])
labels.append((np.array(batch_sample[1].flatten)).transpose())
X_ = np.array(samples)
Y_ = np.array(labels)
X_ = X_[:, :, :, newaxis]
print(X_.shape)
print(Y_.shape)
yield (X_, Y_)
# compile and train the model using the generator function
train_generator = generator(training_data, batch_size = 10)
validation_generator = generator(val_data, batch_size = 10)
run_opts = tf.RunOptions(report_tensor_allocations_upon_oom = True)
model = Sequential()
model.add(Conv2D(32, (4, 4), strides=(2, 2), input_shape = (55, 1756,
1)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Conv2D(16, (2, 2)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Conv2D(8, (2, 2)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Activation('softmax'))
model.add(Flatten()) # this converts our 3D feature maps to 1D feature
vectors
model.add(Dropout(0.3))
model.add(Dense(19316))
model.compile(loss = 'sparse_categorical_crossentropy',
optimizer = 'adam',
metrics = ['accuracy'],
options = run_opts)
model.summary()
batch_size = 20
nb_epoch = 6
model.fit_generator(train_generator,
steps_per_epoch = len(training_data) ,
epochs = nb_epoch,
validation_data = validation_generator,
validation_steps = len(val_data))
解决方案
大概就是这条线
labels.append((np.array(batch_sample[1].flatten)).transpose())
应该是
labels.append((np.array(batch_sample[1].flatten())).transpose())
推荐阅读
- python - json 变成双引号字符串 - python django
- angular - 离子 - 表单无法提交要保存到数据库的数组
- angular - 在 Angular 中填充模型
- laravel - 域元验证
- python - Python f 字符串格式不适用于 strftime 内联
- powershell - 如何在 Office365 中自动删除 Exchange 联系人
- python-3.x - Flask TypeError: argument of type 'NoneType' is not iterable
- android - 将新的 APK 上传到 PlayStore
- git - 与来自不同地点的团队一起在同一个项目上编码
- android - XML 布局与模拟器不同