我正在Tensorflow DNN框架中实现回归模型,该模型采用形状(1504924,127)的输入并预测对应的值(形状(1504924,1)。
以下是我的网络
class q_model:
def __init__(self,
sess,
quantiles,
in_shape=127,
out_shape=1,
batch_size=32):
self.sess = sess
self.quantiles = quantiles
self.num_quantiles = len(quantiles)
self.in_shape = in_shape
self.out_shape = out_shape
self.batch_size = batch_size
self.outputs = []
self.losses = []
self.loss_history = []
self.build_model()
print("Completed")
def build_model(self, scope='q_model', reuse=tf.AUTO_REUSE):
with tf.variable_scope(scope, reuse=reuse) as scope:
self.x = tf.placeholder(tf.float32, shape=(None, self.in_shape,1))
self.y = tf.placeholder(tf.float32, shape=(None, self.out_shape))
self.layer0 = tf.layers.dense(self.x,
units=32,
activation=tf.nn.relu)
self.layer1 = tf.layers.dense(self.layer0,
units=32,
activation=tf.nn.relu)
# Create outputs and losses for all quantiles
for i in range(self.num_quantiles):
q = self.quantiles[i]
# Get output layers
output = tf.layers.dense(self.layer1, 1, name="{}_q{}".format(i, int(q*100)))
self.outputs.append(output)
# Create losses
error = tf.subtract(self.y, output)
loss = tf.reduce_mean(tf.maximum(q*error, (q-1)*error), axis=-1)
self.losses.append(loss)
# Create combined loss
self.combined_loss = tf.reduce_mean(tf.add_n(self.losses))
self.train_step = tf.train.AdamOptimizer().minimize(self.combined_loss)
print("Completed")
def fit(self, x, y, epochs=100):
for epoch in range(epochs):
epoch_losses = []
for idx in range(0, x.shape[1], self.batch_size):
batch_x = x[idx : min(idx + self.batch_size, x.shape[1]),:]
batch_y = y[idx : min(idx + self.batch_size, y.shape[1]),:]
feed_dict = {self.x: batch_x,
self.y: batch_y}
_, c_loss = self.sess.run([self.train_step, self.combined_loss], feed_dict)
epoch_losses.append(c_loss)
epoch_loss = np.mean(epoch_losses)
self.loss_history.append(epoch_loss)
if epoch % 100 == 0:
print("Epoch {}: {}".format(epoch, epoch_loss))
print("Completed")
def predict(self, x):
# Run model to get outputs
feed_dict = {self.x: x}
predictions = sess.run(self.outputs, feed_dict)
return predictions
模型符合要求很好,但是在尝试拟合模型时出现以下错误
TypeError: '(slice(0, 32, None), slice(None, None, None))' is an invalid key
我无法找出我想念的地方。感谢任何输入。