使用张量流时出现错误TypeError

时间:2018-08-30 11:59:22

标签: python python-3.x tensorflow

我是神经网络的新手。我的代码下面一行出现错误

net = tflearn.input_data(shape=[None, len(train_x[0])])

下面是错误,我正在得到”

TypeError: object of type 'numpy.float64' has no len() 

我尝试使用以下语法,但仍然给我一个错误

net = tflearn.input_data(shape=[None, len(train_x)])

我得到的错误:

ValueError: Cannot feed value of shape (8,) for Tensor 'InputData/X:0', which has shape '(?, 19579)'

您能帮我建议怎么办吗?

此外,如果需要,下面是完整的语法

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)


# Input data files are available in the "../input/" directory.
# Any results you write to the current directory are saved as output.
#from subprocess import check_output
#print(check_output(["ls", "../input"]).decode("utf8"))


train = pd.read_csv('C:/Users/gunjit.bedi/Desktop/Tensor Flow/input/train.csv')
print(train.head())

import nltk as nl
train['tokens'] = [nl.word_tokenize(sentences) for sentences in train.text]
words = []
for item in train.tokens:
    words.extend(item)

stemmer = nl.stem.lancaster.LancasterStemmer()
words = [stemmer.stem(word) for word in words]


filtered_words = [word for word in words if word not in nl.corpus.stopwords.words('english')]



import gensim
# let X be a list of tokenized texts (i.e. list of lists of tokens)
model = gensim.models.Word2Vec(filtered_words, size=100)
w2v = dict(zip(model.wv.index2word, model.wv.syn0))

print(w2v['h'])

training = []
for index, item in train.iterrows():
    vec = np.zeros(100)
    token_words = [stemmer.stem(word) for word in item['tokens']]
    token_words = [word for word in token_words if word not in nl.corpus.stopwords.words('english')]
    for w in token_words:
        if w in w2v:
            vec += w2v[w]
    norm = np.linalg.norm(vec)
    if norm != 0:
        vec /= np.linalg.norm(vec)

    training.append(vec)

training_new = np.array(training)

from numpy import array

from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder

# integer encode
label_encoder = LabelEncoder()
integer_encoded = label_encoder.fit_transform(training_new[:,1])

# binary encode
onehot_encoder = OneHotEncoder(sparse=False)
integer_encoded = integer_encoded.reshape(len(integer_encoded), 1)
onehot_encoded = onehot_encoder.fit_transform(integer_encoded)

train_y = onehot_encoded

train_x = list(training_new[:,0])

print(len(train_x))
print(type(train_x))

import tensorflow as tf
import tflearn

# reset underlying graph data
tf.reset_default_graph()
# Build neural network
net = tflearn.input_data(shape=[None, len(train_x[0])])
net = tflearn.fully_connected(net, 8)
net = tflearn.fully_connected(net, 8)
net = tflearn.fully_connected(net, len(train_y), activation='softmax')
net = tflearn.regression(net)

# Define model and setup tensorboard
model = tflearn.DNN(net, tensorboard_dir='tflearn_logs')
# Start training (apply gradient descent algorithm)
model.fit(train_x, train_y, n_epoch=10, batch_size=8, show_metric=True)
model.save('model.tflearn')

2 个答案:

答案 0 :(得分:0)

len()告诉您传递给它的数组的长度。

train_x[0]为您提供train_x数组的 first 元素,该元素没有任何length属性,因此会出现错误消息。

TypeError: object of type 'numpy.float64' has no len() 

这就是为什么删除[0]时不会从len(train_x)收到错误。

我对Tensor Flow并不熟悉,因此无法进一步评论,但这有望解释您的错误原因。

答案 1 :(得分:0)

我能够解决以上问题,以下代码似乎出现了错误

training = []
for index, item in train.iterrows():
    vec = np.zeros(100)
    token_words = [stemmer.stem(word) for word in item['tokens']]
    token_words = [word for word in token_words if word not in nl.corpus.stopwords.words('english')]
    for w in token_words:
        if w in w2v:
            vec += w2v[w]
    norm = np.linalg.norm(vec)
    if norm != 0:
        vec /= np.linalg.norm(vec)

    training.append(vec)

我将其更改为以下内容:检查代码的最后一行

training = []
for index, item in train.iterrows():
    vec = np.zeros(100)
    token_words = [stemmer.stem(word) for word in item['tokens']]
    token_words = [word for word in token_words if word not in nl.corpus.stopwords.words('english')]
    for w in token_words:
        if w in w2v:
            vec += w2v[w]
    norm = np.linalg.norm(vec)
    if norm != 0:
        vec /= np.linalg.norm(vec)
    training.append([vec,item['author']])

该错误是因为未附加“作者”列。 如果tensorflow专家可以确认我的解决方案是否正确,那将是很好的选择。