【问题标题】:when using tensor flow i am getting error TypeError使用张量流时出现错误 TypeError
【发布时间】:2018-08-30 11:59:21
【问题描述】:

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

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


    【解决方案1】:

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

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

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

    这就是为什么当您删除 [0] 时,您不会收到来自 len(train_x) 的错误。

    我不熟悉 Tensor Flow,因此无法进一步评论,但这应该可以解释您的错误来源。

    【讨论】:

    • 您好,我又检查了一遍,去掉[0]后,还是报错。我已经更新了我的帖子
    【解决方案2】:

    我能够解决上述问题,错误似乎在下面的代码中

    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 专家可以确认我的解决方案是否确实正确,那就太好了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-13
      • 2018-06-30
      • 1970-01-01
      • 2020-04-18
      • 2018-06-21
      • 2017-12-27
      • 2016-03-10
      相关资源
      最近更新 更多