AttributeError:在 NLP 任务中调用 model.fit() 时,“tuple”对象没有属性“rank”

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

我正在关注本教程 https://towardsdatascience.com/another-twitter-sentiment-analysis-with-python-part-9-neural-networks-with-tfidf-vectors-using-d0b4af6be6d7

但是,在实现基于 TF-IDF 功能的 ANN 时,我收到此错误 AttributeError:“tuple”对象没有属性“rank”

这是片段-

from sklearn.feature_extraction.text import TfidfVectorizer
tvec1 = TfidfVectorizer(max_features=100000,ngram_range=(1, 3))
tvec1.fit(x_train)


x_train_tfidf = tvec1.transform(x_train)
x_validation_tfidf = tvec1.transform(x_validation).toarray()

seed = 7
np.random.seed(seed)
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout
from tensorflow.keras.layers import Flatten
#from tensorflow.keras.layers.embeddings import Embedding
from tensorflow.keras.layers import Embedding
from tensorflow.keras.preprocessing import sequence

def batch_generator(X_data, y_data, batch_size):
    samples_per_epoch = X_data.shape[0]
    number_of_batches = samples_per_epoch/batch_size
    counter=0
    index = np.arange(np.shape(y_data)[0])
    while 1:
        index_batch = index[batch_size*counter:batch_size*(counter+1)]
        X_batch = X_data[index_batch,:].toarray()
        y_batch = y_data[y_data.index[index_batch]]
        counter += 1
        yield X_batch,y_batch
        if (counter > number_of_batches):
            counter=0

model = Sequential()
model.add(Dense(64, activation='relu', input_dim=100000))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['accuracy'])

model.fit(batch_generator(x_train_tfidf, y_train, 32), epochs=5, validation_data=(x_validation_tfidf, y_validation),steps_per_epoch=x_train_tfidf.shape[0]/32)

这是错误-

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_13000\1276649087.py in <module>
      1 model.fit(batch_generator(x_train_tfidf, y_train, 32),
      2                     epochs=5, validation_data=(x_validation_tfidf, y_validation),
----> 3                     steps_per_epoch=x_train_tfidf.shape[0]/32)

~\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
   1145           use_multiprocessing=use_multiprocessing,
   1146           model=self,
-> 1147           steps_per_execution=self._steps_per_execution)
   1148 
   1149       # Container that configures and calls `tf.keras.Callback`s.

~\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\engine\data_adapter.py in get_data_handler(*args, **kwargs)
   1362   if getattr(kwargs["model"], "_cluster_coordinator", None):
   1363     return _ClusterCoordinatorDataHandler(*args, **kwargs)
-> 1364   return DataHandler(*args, **kwargs)
   1365 
   1366 

~\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\engine\data_adapter.py in __init__(self, x, y, sample_weight, batch_size, steps_per_epoch, initial_epoch, epochs, shuffle, class_weight, max_queue_size, workers, use_multiprocessing, model, steps_per_execution, distribute)
   1164         use_multiprocessing=use_multiprocessing,
   1165         distribution_strategy=ds_context.get_strategy(),
-> 1166         model=model)
   1167 
   1168     strategy = ds_context.get_strategy()

~\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\engine\data_adapter.py in __init__(self, x, y, sample_weights, workers, use_multiprocessing, max_queue_size, model, **kwargs)
    826       return tensor_shape.TensorShape([None for _ in shape.as_list()])
    827 
--> 828     output_shapes = nest.map_structure(_get_dynamic_shape, peek)
    829     output_types = nest.map_structure(lambda t: t.dtype, peek)
    830 

~\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\util\nest.py in map_structure(func, *structure, **kwargs)
    865 
    866   return pack_sequence_as(
--> 867       structure[0], [func(*x) for x in entries],
    868       expand_composites=expand_composites)
    869 

~\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\util\nest.py in <listcomp>(.0)
    865 
    866   return pack_sequence_as(
--> 867       structure[0], [func(*x) for x in entries],
    868       expand_composites=expand_composites)
    869 

~\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\engine\data_adapter.py in _get_dynamic_shape(t)
    822       shape = t.shape
    823       # Unknown number of dimensions, `as_list` cannot be called.
--> 824       if shape.rank is None:
    825         return shape
    826       return tensor_shape.TensorShape([None for _ in shape.as_list()])

AttributeError: 'tuple' object has no attribute 'rank'
python keras neural-network nlp tf-idf
1个回答
0
投票

当函数需要张量,但传递的是其他类型的数据时,就会发生这种情况。 例如,numpy 数组,如下所示:

y.shape

输出:(2000,)

y.shape.rank

out:AttributeError:'tuple'对象没有属性'rank'

要纠正此问题,应将数据转换为张量。

tf.constant(y).shape.rank

输出:1

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