Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当使用张量流时,我得到错误类型错误_Python_Python 3.x_Tensorflow - Fatal编程技术网

Python 当使用张量流时,我得到错误类型错误

Python 当使用张量流时,我得到错误类型错误,python,python-3.x,tensorflow,Python,Python 3.x,Tensorflow,我不熟悉神经网络。我的代码中的下一行出现错误 net=tflearn.input_数据(shape=[None,len(train_x[0])) 下面是错误,我得到“ 我尝试了下面的语法,但它仍然给我一个错误 net=tflearn.输入数据(shape=[None,len(train\u x)]) 我得到的错误是: ValueError:无法为张量'InputData/X:0'输入形状(8,)的值,该张量具有形状'(?,19579) 你能建议我该怎么做吗 此外,如果需要,下面是完整的语法 im

我不熟悉神经网络。我的代码中的下一行出现错误

net=tflearn.input_数据(shape=[None,len(train_x[0]))

下面是错误,我得到“

我尝试了下面的语法,但它仍然给我一个错误

net=tflearn.输入数据(shape=[None,len(train\u x)])

我得到的错误是:

ValueError:无法为张量'InputData/X:0'输入形状(8,)的值,该张量具有形状'(?,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')
len()
告诉您传递给它的数组的长度

train\u x[0]
获取
train\u x
数组的第一个元素,该数组没有任何长度属性,因此会显示错误消息

TypeError: object of type 'numpy.float64' has no len() 
这就是为什么当您删除
[0]
时,不会从
len(train\u x)
中得到错误


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

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

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

您好,我再次检查了,删除[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,item['author']])