在keras中训练nn时使用序列设置数组元素

时间:2019-06-25 19:13:16

标签: machine-learning keras neural-network deep-learning keras-layer

我遇到了错误 在keras中训练我的神经网络时出现“使用序列设置数组元素”错误。

我的输入xtrain [0]是(1,300)并属于列表。 它是使用w2v构成的句子向量。 我尝试将其更改为numpy array并重塑,但没有帮助。 我在第一个密集层之后使用了一个平坦层,但是落在错误平坦上需要3维输入。

我已经搜索了几乎所有线程。帮帮我 !!

带有句子和标签的数据集。 我通过查找各个标记的均值来转换每个句子。 一热编码标签。 将两者都传递到两层神经网络的拟合方法中。 xtrain的元素是一个长度为300的列表

import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split

from keras.models import Sequential 
from keras.layers import Dense 
from sklearn.preprocessing import LabelEncoder,OneHotEncoder,MinMaxScaler
from sklearn.compose import ColumnTransformer
from keras.utils.np_utils import to_categorical
from keras import optimizers
from sklearn.utils import shuffle
import spacy

nlp = spacy.load('en_core_web_lg')

data = pd.read_csv('dialog.csv',encoding='ISO-8859-1')

x = data.Dialogs #List of sentences
y = data.Label 

# To find the vectors of sentence using w2v
def meanv(coords):
    # assumes every item in coords has same length as item 0
    sumv = [0] * len(coords[0])
    for item in coords:
        for i in range(len(item)):
            sumv[i] += item[i]
    mean = [0] * len(sumv)
    for i in range(len(sumv)):
        mean[i] = float(sumv[i]) / len(coords)
    return mean

def sentvec(s):
    print(s)
    for i in range(0,len(s)):
        sent = nlp(s[i])
        x[i] = meanv([w.vector for w in sent])

sentvec(x)

# To change the list vector to array
for i in range(0,len(x)):
    x[i] = np.array(x[i])


enc = LabelEncoder()
y = enc.fit_transform(y)
y = to_categorical(y)

input_dim = len(data.columns) - 1

model = Sequential()
model.add(Dense(16, input_dim = input_dim , activation = 'relu'))
model.add(Dense(10, activation = 'relu'))
model.add(Dense(10, activation = 'relu'))
model.add(Dense(5, activation = 'softmax'))

opt = optimizers.SGD(lr=0.01, momentum=0.9)
model.compile(loss = 'categorical_crossentropy' , optimizer = opt , metrics = ['categorical_accuracy'] )


model.fit(x,y, epochs = 100, batch_size = 10)

scores = model.evaluate(test_x, test_y)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))

pred1 = model.predict_classes(test_x)
pred = model.predict(test_x)

0 个答案:

没有答案