AttributeError:“ str”对象没有属性“ ndim”

艾米:

我正在使用Keras实施情感分析代码。我的训练数据如下:

  • pos.txt:所有肯定评论的文本文件,以行分隔
  • neg.txt:所有负面评论的文本文件,以行分隔

我以与此处类似的方式构建代码

唯一的区别是它们的数据是从Keras数据集导入的,而我的是文本文件

这是我的代码

# CNN for the IMDB problem

top_words = 5000

pos_file=open('pos.txt', 'r')
neg_file=open('neg.txt', 'r')
 # Load data from files
 pos = list(pos_file.readlines())
 neg = list(neg_file.readlines())
 x = pos + neg
 total = numpy.array(x)
 # Generate labels
 positive_labels = [1 for _ in pos]
 negative_labels = [0 for _ in neg]
 y = numpy.concatenate([positive_labels, negative_labels], 0)

 #Testing
 pos_test=open('posTest.txt', 'r')
 posT = list(pos_test.readlines())
 print("pos length is",len(posT))

 neg_test=open('negTest.txt', 'r')
 negT = list(neg_test.readlines())
 xTest = pos + negT
 total2 = numpy.array(xTest)

# Generate labels
positive_labels2 = [1 for _ in posT]
negative_labels2 = [0 for _ in negT]
yTest = numpy.concatenate([positive_labels2, negative_labels2], 0)

#Create model
max_words = 1
model = Sequential()
model.add(Embedding(top_words, 32, input_length=max_words))

model.add(Conv1D(filters=32, kernel_size=3, padding='same', activation='relu'))
model.add(MaxPooling1D(pool_size=1))
model.add(Flatten())
model.add(Dense(250, activation='relu'))

model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model.summary())

#Fit the model

model.fit(total, y, validation_data=(xTest, yTest), epochs=2, batch_size=128, verbose=2)

# Final evaluation of the model
scores = model.evaluate(total2, yTest, verbose=0)
print("Accuracy: %.2f%%" % (scores[1]*100))

运行代码时,出现此错误

File "C:\Users\\Anaconda3\lib\site-packages\keras\engine\training.py", line 70, in <listcomp>
data = [np.expand_dims(x, 1) if x is not None and x.ndim == 1 else x for x in data]

AttributeError: 'str' object has no attribute 'ndim'
S.Mohsen sh:

您正在向模型提供字符串列表,这是我们所不希望的。您可以使用keras.preprocessing.text模块将文本转换为整数序列。更具体地说,您可以准备如下数据:

from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
tk = Tokenizer()
tk.fit_on_texts(texts)
index_list = tk.texts_to_sequences(texts)
x_train = pad_sequences(index_list, maxlen=maxlen)

现在x_trainn_samples * maxlen类型ndarray np.int)是模型的合法输入。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Keras:AttributeError:“ int”对象在使用model.fit时没有属性“ ndim”

Keras AttributeError:“列表”对象没有属性“ ndim”

AttributeError(“'str'对象没有属性'read'”)

AttributeError(“'str'对象没有属性'read'”)

AttributeError:“ str”对象没有属性“ decode”

AttributeError:'str'对象没有属性'fileno'

/'str'对象的AttributeError没有属性'objects'

错误-AttributeError:在keras的自动编码器设计中,“ DirectoryIterator”对象没有属性“ ndim”

AttributeError:'str'对象没有属性'isnumeric'

AttributeError:'str'对象没有属性'ndim',无法使用model.predict()

AttributeError:'str'对象没有属性'str'

AttributeError:'str'对象没有属性'author'

AttributeError:'str'对象没有属性sleep

AttributeError:'str'对象没有属性'xpath'

AttributeError:'str'对象没有属性'errno'

attributeError:'str'对象没有属性'dbname'

AttributeError:“ str”对象没有属性“ values”

AttributeError:'str'对象没有属性'union'

python中的skimage的rgb2gray:AttributeError:Nonetype对象没有属性ndim

AttributeError:“ str”对象没有属性“ name”

AttributeError:“布尔”对象没有属性“ ndim”

Keras/Theano VGG16 AttributeError: 'Model' 对象没有属性 'ndim'

AttributeError: 'str' 对象没有属性 'keys'

AttributeError: 'str' 对象没有属性 'loc'

AttributeError: 'str' 对象没有属性 'mode'

AttributeError: 'str' 对象没有属性 'map'

AttributeError: 'str' 对象没有属性 'client'

AttributeError: 'str' 对象没有属性 'fetchone'

AttributeError: 'list' 对象在 ax.plt_wireframe() 中没有属性 'ndim'