【发布时间】:2018-03-24 18:09:17
【问题描述】:
经过几次不成功的尝试,我想请您帮忙解决这个错误。 我正在尝试通过使用本地 csv 文件来训练深度自动编码器网络,然后将其(由 csv 和 numpy 库)转换为 numpy 数组。但是这些数据永远不会输入到我的占位符的张量中。
这是深度自动编码器的摘要:
class Deep_Autoencoder:
def __init__(self, input_dim, n_nodes_hl = (32, 16, 1), epochs = 400, batch_size = 128, learning_rate = 0.02, n_examples = 10):
# Hyperparameters
self.input_dim = input_dim
self.epochs = epochs
self.batch_size = batch_size
self.learning_rate = learning_rate
self.n_examples = n_examples
# Input and target placeholders
X = tf.placeholder('float', [None, self.input_dim])
Y = tf.placeholder('float', [None, self.input_dim])
...
self.X = X
print("self.X : ", self.X)
self.Y = Y
print("self.Y : ", self.Y)
...
def train_neural_network(self, data, targets):
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(self.epochs):
epoch_loss = 0
i = 0
# Let's train it in batch-mode
while i < len(data):
start = i
end = i + self.batch_size
batch_x = np.array(data[start:end])
print("type batch_x :", type(batch_x))
print("len batch_x :", len(batch_x))
batch_y = np.array(targets[start:end])
print("type batch_y :", type(batch_y))
print("len batch_y :", len(batch_y))
hidden, _, c = sess.run([self.encoded, self.optimizer, self.cost], feed_dict={self.X: batch_x, self.Y: batch_y})
epoch_loss +=c
i += self.batch_size
self.saver.save(sess, 'selfautoencoder.ckpt')
print('Accuracy', self.accuracy.eval({self.X: data, self.Y: targets}))
我在这里创建输入数据,您可以在下面看到我将打印出它们的主要特征以供您参考(请注意,我实际上只对第 3 列感兴趣):
features_DeepAE = create_feature_sets(filename)
Train_x = np.array(features_DeepAE[0])
Train_y = np.array(features_DeepAE[1])
print("type Train_x : ", type(Train_x))
print("type Train_x.T[3] : ", type(Train_x.T[3]))
print("len Train_x : ", len(Train_x))
print("len Train_x.T[3] : ", len(Train_x.T[3]))
print("shape Train_x : ", Train_x.shape)
print("type Train_y : ", type(Train_y))
print("type Train_y.T[3] : ", type(Train_y.T[3]))
print("len Train_y : ", len(Train_y))
print("len Train_y.T[3] : ", len(Train_y.T[3]))
print("shape Train_y : ", Train_y.shape)
我在这里运行代码:
DAE = Deep_Autoencoder(input_dim = len(Train_x))
DAE.train_neural_network(Train_x.T[3], Train_y.T[3])
这些是打印输出,仅供参考:
type Train_x : <class 'numpy.ndarray'>
type Train_x.T[3] : <class 'numpy.ndarray'>
len Train_x : 3433
len Train_x.T[3] : 3433
shape Train_x : (3433, 5)
type Train_y : <class 'numpy.ndarray'>
type Train_y.T[3] : <class 'numpy.ndarray'>
len Train_y : 3433
len Train_y.T[3] : 3433
shape Train_y : (3433, 5)
self.X : Tensor("Placeholder_142:0", shape=(?, 3433), dtype=float32)
self.Y : Tensor("Placeholder_143:0", shape=(?, 3433), dtype=float32)
type batch_x : <class 'numpy.ndarray'>
len batch_x : 128
type batch_y : <class 'numpy.ndarray'>
len batch_y : 128
最后是错误:
ValueError:无法为形状为“(?, 3433)”的张量“Placeholder_142:0”提供形状 (128,) 的值
是的......我在占位符#143......这意味着很多失败(重塑批次和/或张量,转置一个和/或另一个,在互联网上寻找解决方法......) ! 如果需要,请随时询问更多信息。
【问题讨论】:
-
你能打印其中一批来看看它的形状吗?
-
你能把你的代码上传到某个地方吗?如果你这样做,我会尝试运行它。
-
也许打印
batch_x.shape -
根据我对您代码的理解,您正在尝试对单个功能进行编码解码。因此,更改
input_dim=1而不是将其设置为训练集中的样本数。此外,如果是这种情况,编码-解码单个特征没有意义.. -
按照@amirbar 的建议 - 将您的 input_dim 设置为 1 并为您的 batch_x 和 batch_y 添加一个“虚拟”维度,以使它们适合形状 [?, 1] 的占位符,如下所示:@ 987654327@ 和
batch_y = np.array(targets[start:end])[:, None]。 None 向 Numpy 数组添加一个空维度。
标签: python numpy tensorflow