asl*_*sli 7 python keras tensorflow
我是 python 初学者。我尝试进行情感分析和RNN。但是,我AttributeError: \'str\' object has no attribute \'shape\'".查看了有关此问题的所有已发布解决方案,但无法解决此问题。我尝试在另一个数据文件中使用相同的代码,它可以工作。但不适用于我的原始数据文件。
这是我的代码:
\n\nimport numpy as np\nimport pandas as pd\nfrom tensorflow.python.keras.models import Sequential`\nfrom tensorflow.python.keras.layers import Dense, GRU, Embedding, CuDNNGRU\nfrom tensorflow.python.keras.optimizers import Adam\nfrom tensorflow.python.keras.preprocessing.text import Tokenizer\nfrom tensorflow.python.keras.preprocessing.sequence import pad_sequences\n\n\ndataset = pd.read_csv(r\'C:\\Users\\Administrator\\Desktop\\t\xc3\xbcmveri8.csv\', encoding=\'latin1\')\n\ntarget = dataset[\'duygu\'].values.tolist()\ndata = dataset[\'yorum\'].values.tolist()\n\ncutoff = int(len(data) * 0.80)\nx_train, x_test = data[:cutoff], data[cutoff:]\ny_train, y_test = target[:cutoff], target[cutoff:]\n\nnum_words = 10000\ntokenizer = Tokenizer(num_words=num_words)\ntokenizer.fit_on_texts(data)\n\nx_train_tokens = tokenizer.texts_to_sequences(x_train)\n\nx_test_tokens = tokenizer.texts_to_sequences(x_test)\n\nnum_tokens = [len(tokens) for tokens in x_train_tokens + x_test_tokens]\nnum_tokens = np.array(num_tokens)\nmax_tokens = np.mean(num_tokens) + 2 * np.std(num_tokens)\nmax_tokens = int(max_tokens)\nmax_tokens\n\nnp.sum(num_tokens < max_tokens) / len(num_tokens)\nx_train_pad = pad_sequences(x_train_tokens, maxlen=max_tokens)\nx_test_pad = pad_sequences(x_test_tokens, maxlen=max_tokens)\n\nidx = tokenizer.word_index\ninverse_map = dict(zip(idx.values(), idx.keys()))\ndef tokens_to_string(tokens):\nwords = [inverse_map[token] for token in tokens if token!=0]\ntext = \' \'.join(words)\nreturn text\n\nmodel = Sequential()\nembedding_size = 50\nmodel.add(Embedding(input_dim=num_words,\n output_dim=embedding_size,\n input_length=max_tokens,\n name=\'embedding_layer\'))\n\nmodel.add(GRU(units=16, return_sequences=True))\nmodel.add(GRU(units=8, return_sequences=True))\nmodel.add(GRU(units=4))\nmodel.add(Dense(1, activation=\'sigmoid\'))\n\noptimizer = Adam(lr=1e-3)\n\nmodel.compile(loss=\'binary_crossentropy\',\n optimizer=optimizer,\n metrics=[\'accuracy\'])\nmodel.summary()\nRun Code Online (Sandbox Code Playgroud)\n\n这是错误代码:
\n\nmodel.fit(x_train_pad, y_train, epochs=5, batch_size=256)\n\n\nmodel.fit(x_train_pad, y_train, epochs=5, batch_size=256)\n\nAttributeError Traceback (most recent call \nlast)\n<ipython-input-79-631bbf0ac3a7> in <module>\n----> 1 model.fit(x_train_pad, y_train, epochs=5, batch_size=256)\n\n~\\Anaconda3\\lib\\site-packages\\tensorflow\\python\\keras\\engine\\training.py \nin fit(self, x, y, batch_size, epochs, verbose, callbacks, \nvalidation_split, validation_data, shuffle, class_weight, sample_weight, \ninitial_epoch, steps_per_epoch, validation_steps, validation_freq, \nmax_queue_size, workers, use_multiprocessing, **kwargs)\n707 steps=steps_per_epoch,\n708 validation_split=validation_split,\n--> 709 shuffle=shuffle)\n710 \n711 # Prepare validation data.\n\n~\\Anaconda3\\lib\\site-packages\\tensorflow\\python\\keras\\engine\\training.py \nin _standardize_user_data(self, x, y, sample_weight, class_weight, \nbatch_size, check_steps, steps_name, steps, validation_split, shuffle, \nextract_tensors_from_dataset)\n2671 shapes=None,\n2672 check_batch_axis=False, # Don\'t enforce the batch size.\n-> 2673 exception_prefix=\'target\')\n2674 \n2675 # Generate sample-wise weight values given the `sample_weight` \nand\n\n~\\Anaconda3\\lib\\site- \npackages\\tensorflow\\python\\keras\\engine\\training_utils.py in \nstandardize_input_data(data, names, shapes, check_batch_axis, \nexception_prefix)\n335 ]\n336 else:\n--> 337 data = [standardize_single_array(x) for x in data]\n338 \n339 if len(data) != len(names):\n\n~\\Anaconda3\\lib\\site- \n packages\\tensorflow\\python\\keras\\engine\\training_utils.py in <listcomp> (.0)\n335 ]\n336 else:\n--> 337 data = [standardize_single_array(x) for x in data]\n338 \n339 if len(data) != len(names):\n\n~\\Anaconda3\\lib\\site- \npackages\\tensorflow\\python\\keras\\engine\\training_utils.py in \nstandardize_single_array(x, expected_shape)\n263 return None\n264 \n--> 265 if (x.shape is not None and len(x.shape) == 1 and\n266 (expected_shape is None or len(expected_shape) != 1)):\n267 if tensor_util.is_tensor(x):\n\nAttributeError: \'str\' object has no attribute \'shape\'\nRun Code Online (Sandbox Code Playgroud)\n
小智 2
这一行有一个错误:
model.fit(x_train_pad, y_train, epochs=5, batch_size=256)
Basically x_train_padis an str (string) an it should be a numpy array
| 归档时间: |
|
| 查看次数: |
29805 次 |
| 最近记录: |