【发布时间】:2022-01-15 16:04:57
【问题描述】:
我在使用tensorflow2.0的时候遇到了一些问题,这是我的代码,控制台报错:
x=tf.reshape(x,shape=[BATCH_SIZE, self.lstm_step_num,self.input_length*self.vector_length])
File"/home/cyye/anaconda3/lib/python3.7/site-packages/te nsorflow/python/ops/array_ops.py", line 193, in reshape
result = gen_array_ops.reshape(tensor, shape, name)
File"/home/cyye/anaconda3/lib/python3.7/site-packages/te nsorflow/python/ops/gen_array_ops.py", line 8080, in reshape
tensor, shape, name=name, ctx=_ctx)
File"/home/cyye/anaconda3/lib/python3.7/site-packages/te nsorflow/python/ops/gen array ops.py", line 8107, in reshape eager fallback
ctx=ctx,name=name)
File"/home/cyye/anaconda3/lib/python3.7/site-packages/te nsorflow/python/eager/execute.py", line 60, in quick_execute
inputs, attrs, num_outputs)
tensorflow.python.framework.errors_impl.InvalidArgumenterro r: Input to reshape is a tensor with 405000 values, but the reques ted shape has 1920000[0p:Reshape]
我的数据集的shape=[4999990,10,15],类型是list。在 reshape 层,input(128,10,15,100)(1920000) 的数据 rashape 为 data(BATCH_SIZE=128, self.lstm_step_num=10, self.input_length=15,self. vector_length=100)(1920000)。 我不知道错误在哪里,为什么不能转换形状。我非常感谢有关如何清理我的代码的任何提示!提前致谢!
# author:ZhuYuYing
# data:2021/12/9
# projectName:repetition-QSPC
import tensorflow as tf
import numpy as np
import tensorflow.keras as keras
from tensorflow.keras import layers
import time
tf.keras.backend.set_floatx('float32')
dataSet = np.load(r"../data/yuan/train_four/dataset_1.npy").astype('float32')
labels = np.load(r"../data/yuan/train_four/dataset_1_label_rt.npy").astype('float32')
data_rate = 0.5
split_data = int(len(dataSet)*data_rate)
train_data, test_data = dataSet[:split_data], dataSet[split_data:]
train_labels, test_labels = labels[:split_data], labels[split_data:]
BATCH_SIZE = 128
train_dataset = tf.data.Dataset.from_tensor_slices((train_data, train_labels))
train_dataset = train_dataset.shuffle(buffer_size=1024).batch(BATCH_SIZE)
test_dataset = tf.data.Dataset.from_tensor_slices((test_data, test_labels))
test_dataset = test_dataset.batch(BATCH_SIZE)
class TenModel(keras.Model):
def __init__(self,UUID,vector_length,input_length,lstm_step_num,lstm_unit_num,
rate,perception_unit_num,ts_unit_num):
super(TenModel, self).__init__()
self.uuid = UUID
self.vector_length = vector_length
self.input_length = input_length
self.lstm_step_num = lstm_step_num
self.lstm_unit_num = lstm_unit_num
self.rate = rate
self.perception_unit_num = perception_unit_num
self.ts_unit_num = ts_unit_num
def call(self, inputs, training=None, mask=None):
x = layers.Embedding(self.uuid + 1, self.vector_length, embeddings_initializer='normal', input_length=self.input_length)(inputs) # 10*15*100
#x = layers.Reshape((BATCH_SIZE, self.lstm_step_num, self.input_length*self.vector_length))(x)
x = tf.reshape(x, shape=[BATCH_SIZE, self.lstm_step_num, self.input_length*self.vector_length])
x = layers.Dense(512, activation=tf.nn.swish, bias_initializer=tf.random_normal_initializer())(x)
x = tf.nn.dropout(x,rate=self.rate)
x = layers.Dense(self.vector_length, activation=tf.nn.swish, bias_initializer=tf.random_normal_initializer())(x)
x = layers.Dropout(self.rate)(x)
lstm_x, _, _ = layers.LSTM(self.lstm_unit_num, return_sequences=True, return_state=True, activation=tf.nn.swish)(x)
x = lstm_x[:, -1:, :]
#x = layers.Reshape((BATCH_SIZE, self.lstm_unit_num))(x)
x = tf.reshape(x, shape=[BATCH_SIZE, self.lstm_unit_num])
x = layers.Dense(self.perception_unit_num, activation=tf.nn.swish, bias_initializer=tf.random_normal_initializer())(x)
x = layers.Dropout(self.rate)(x)
x = layers.Dense(self.perception_unit_num, activation=tf.nn.swish, bias_initializer=tf.random_normal_initializer())(x)
x = layers.Dropout(self.rate)(x)
x = layers.Dense(self.ts_unit_num, activation=tf.nn.swish, bias_initializer=tf.random_normal_initializer())(x)
attention_x = layers.Dense(self.ts_unit_num, activation=tf.nn.swish, bias_initializer=tf.random_normal_initializer())(tf.concat([tf.reshape(lstm_x, [BATCH_SIZE, -1]), x], axis=1))
attention_x = layers.Dense(self.ts_unit_num, activation=tf.nn.swish, bias_initializer=tf.random_normal_initializer())(attention_x)
x = tf.multiply(x, tf.nn.softmax(attention_x))
#x = tf.reduce_sum(x,axis=1)
x = layers.Dense(self.ts_unit_num/2, activation=tf.nn.swish, bias_initializer=tf.random_normal_initializer())(x)
return x
def loss_fn(y_true, y_pred):
return tf.keras.losses.MSE(y_true, y_pred)
optimizer = tf.keras.optimizers.Adam()
train_loss = tf.keras.metrics.Mean(name='train_loss')
train_acc = tf.keras.metrics.SparseCategoricalAccuracy(name="train_acc")
test_loss = tf.keras.metrics.Mean(name='test_loss')
test_acc = tf.keras.metrics.SparseCategoricalAccuracy(name='test_acc')
'''
self,UUID,vector_length,input_length,lstm_step_num,lstm_unit_num,
rate,perception_unit_num,ts_unit_num
11212,100,0.001,256,256,10,128,256
'''
Model = TenModel(11212,100,15,10,256,0.1,256,256)
tf.config.run_functions_eagerly(True)
@tf.function
def train_step(train_data, train_labels):
with tf.GradientTape() as tape:
y_pre = Model(train_data)
print(y_pre)
loss = loss_fn(train_labels, y_pre)
total_loss = loss + sum(Model.losses)
gradients = tape.gradient(total_loss, Model.trainable_variables)
optimizer.apply_gradients(zip(gradients, Model.trainable_variables))
train_loss(loss)
train_acc(train_labels, y_pre)
@tf.function
def test_step(test_data, test_labels):
y_pre = Model(test_data)
loss = loss_fn(test_labels, y_pre)
test_loss(loss)
test_acc(test_labels, y_pre)
EPOCHS = 10
for epoch in range(EPOCHS):
start = time.time()
for train_data, train_labels in train_dataset:
train_step(train_data, train_labels)
for test_data, test_labels in test_dataset:
test_step(test_data, test_labels)
print ('Time for epoch {} is {} sec'.format(epoch + 1, time.time()-start))
print('Epoch:{},Loss:{},Acc:{},Test Loss:{},Test Acc:{}'.format(epoch + 1, train_loss.result(),
train_acc.result() * 100, test_loss.result(),
test_acc.result() * 100))
print(Model.summary())
tf.keras.utils.plot_model(Model, "my_model_info.png", show_shapes=True)
Model.save("my_model")
【问题讨论】:
标签: python deep-learning tensorflow2.0