python-DatasetV1Adapter对象不可订阅
发布时间:2022-03-04 13:51:07 247
相关标签: # node.js
我想编程一个神经网络。首先,我有自己的数据集,希望使用嵌入层,因为我有文本数据。
我的数据集(功能和标签)如下所示:
这就是我到目前为止所做的:
from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
import pandas as pd
from tensorflow import keras
from tensorflow.python.keras import layers
from google.colab import drive
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
tf.enable_eager_execution()
# Convert to a tensorflow dataset
drive.mount('/content/drive')
training_df: pd.DataFrame = pd.read_csv("/content/drive/MyDrive/Data.csv")
features = ['user', 'query', 'weight','word']
training_df.head()
training_df.info()
training_dataset = (
tf.data.Dataset.from_tensor_slices(
(
tf.cast(training_df['user'].values, tf.float32),
tf.cast(training_df['query'].values, tf.string),
tf.cast(training_df['weight'].values, tf.float32),
tf.cast(training_df['word'].values, tf.string),
tf.cast(training_df['label'].values, tf.float32),
)
)
)
vocab_size = 10000
# A dictionary mapping words to an integer index
word_index = training_dataset['query'].get_word_index()
# The first indices are reserved
word_index = {k: (v+3) for k, v in word_index.items()}
word_index[""] = 0
word_index[""] = 1
word_index[""] = 2 # unknown
word_index[""] = 3
reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])
def decode_review(text):
return ' '.join([reverse_word_index.get(i, '?') for i in text])
maxlen = 500
train_data = keras.preprocessing.sequence.pad_sequences(train_data,
value=word_index[""],
padding='post',
maxlen=maxlen)
test_data = keras.preprocessing.sequence.pad_sequences(test_data,
value=word_index[""],
padding='post',
maxlen=maxlen)
embedding_dim = 16
model = keras.Sequential([
layers.Embedding(vocab_size, embedding_dim, input_length=maxlen),
layers.GlobalAveragePooling1D(),
layers.Dense(16, activation='relu'),
layers.Dense(1, activation='sigmoid')
])
model.summary()
model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
history = model.fit(
train_data,
train_labels,
epochs=30,
batch_size=512,
validation_split=0.2)
但是,我收到了以下错误消息:
Traceback (most recent call last)
in ()
39 # A dictionary mapping words to an integer index
40
---> 41 word_index = training_dataset['query'].get_word_index()
42
43 # The first indices are reserved
TypeError: 'DatasetV1Adapter' object is not subscriptable
我做错了什么?我该怎么做?
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报