当使用张量流我得到错误TypeError

问题描述 投票:0回答:2

我是神经网络的新手。我在代码中的下面一行收到错误

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')
python python-3.x tensorflow
2个回答
0
投票

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

train_x[0]为您提供train_x数组的第一个元素,该数组没有任何长度属性,因此出现错误消息。

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

这就是为什么当你删除[0]时,你没有从len(train_x)得到错误。

我不熟悉Tensor Flow,所以不能进一步评论,但这应该有助于解释你的错误来源。


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']])

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

© www.soinside.com 2019 - 2024. All rights reserved.