python - 错误:super(type, obj) obj 必须是 type 的实例或子类型
问题描述
请协助使用机器学习模型对我的分类代码进行故障排除。它抛出以下错误。代码的主要本质是使用 imdb 数据训练模型,以正确分类正面评论和负面评论。
File "/Users/darlingtonemeagi/Downloads/untitled14.py", line 49, in <module>
model = models.Sequential()
File "/anaconda3/lib/python3.5/site-packages/keras/engine/sequential.py", line 87, in __init__
super(Sequential, self).__init__(name=name)
File "/anaconda3/lib/python3.5/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "/anaconda3/lib/python3.5/site-packages/keras/engine/network.py", line 96, in __init__
self._init_subclassed_network(**kwargs)
File "/anaconda3/lib/python3.5/site-packages/keras/engine/network.py", line 300, in _init_subclassed_network
self._base_init(name=name)
File "/anaconda3/lib/python3.5/site-packages/keras/engine/network.py", line 110, in _base_init
self.name = name
File "/anaconda3/lib/python3.5/site-packages/keras/engine/network.py", line 322, in __setattr__
super(Network, self).__setattr__(name, value)
TypeError: super(type, obj): obj must be an instance or subtype of type"
我查看了关键代码,但无法发现问题出在哪里。
from keras import models
from keras import layers
from keras.datasets import imdb
import numpy as np
(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)
#Vectorised the data using Hot-encode technology
def vectorise_sequence(sequences, dimension=10000):
results = np.zeros((len(sequences), dimension)) #Create zeros matrix of shape len(sequences) and dimension
for i, sequence in enumerate(sequences):
results[i, sequence] = 1 # set specific indices of results[i] to 1
return results
#Vectorise training and test data
x_train = vectorise_sequence(train_data)
x_test = vectorise_sequence(test_data)
#Also vectorising the labels which is also straight forward
y_train = np.asarray(train_labels).astype('float32')
y_test = np.asarray(test_labels).astype('float32')
#The inputs data are vectors while the output data is scalar (0 and 1)
#Set aside a validation set: Data the model has never seen before in other to monitor
#the accuracy of the model
x_val= x_train[:10000]
partial_x_train = x_train[10000:]
y_val = y_train[:10000]
partial_y_train = y_train[10000:]
#Building the Network Model
model = models.Sequential()
model.add(layers.Dense(16, activation='relu', input_size=(10000,)))
model.add(layers.Dense(16, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))
#Compiling the network
model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['acc'])
history = model.fit(partial_x_train, partial_y_train, epochs=20,\
batch_size=512, validation_data = (x_val,y_val))
history_dict = history.history
print(history_dict.keys())
import matplotlib.pyplot as plt
loss_values = history_dict['loss']
val_loss_values = history_dict['val_loss']
epochs = range(1, 20)
plt.plot(epochs, loss_values, 'bo', label='Training Loss')
plt.plot (epochs, val_loss_values, 'b', label='Validation Loss')
plt.title('Training and Validation Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.Legend()
plt.show()
预期输出是训练损失和验证损失的图形表示。
解决方案
推荐阅读
- ios - MKGeoJSONDecoder 无法解析转义的双引号字符
- iis - IIS URL 将文件名的一部分重写为文件夹
- javascript - 所有对象方法中的 1 个不适用于 eventHandler 鼠标单击
- c# - 如何按升序对字典进行排序?
- google-finance - GOOGLEFINANCE - 如何获取阿姆斯特丹数据?
- azure-active-directory - 将服务主体所有者更改为组
- vue.js - 我在为我的 vue 项目提供服务时无法找到的清单 json 中出现错误
- php - 按产品类别隐藏运输方式,如果不是
- javascript - 通过单击第一个 HTML 页面中的按钮填充第二个 HTML 页面的下拉列表
- woocommerce - 将产品添加到购物车时,是否有一个简单的功能可以自动降低库存水平?- 呜