【发布时间】:2017-07-06 19:11:41
【问题描述】:
以下 sn-p 来自相当大的一段代码,但希望我能提供所有必要的信息:
y2 = tf.matmul(y1,ymask)
dist = tf.norm(ystar-y2,axis=0)
y1 和 y2 是 128x30,ymask 是 30x30。 ystar 是 128x30。距离是 1x30。当 ymask 是单位矩阵时,一切正常。但是,当我将它设置为全零时,除了沿对角线的单个 1(以便将 y2 中除一之外的所有列设置为零)外,我使用 tf.梯度(距离,[y2])。 dist 的具体值为 [0,0,7.9,0,...],所有 ystar-y2 值都在第三列的范围 (-1,1) 附近,其他地方为零。
鉴于没有日志或除法,我很困惑为什么这里会出现数字问题,这是下溢吗?我在数学中遗漏了什么吗?
对于上下文,我这样做是为了尝试使用整个网络训练 y 的各个维度,一次一个。
要复制的更长版本:
import tensorflow as tf
import numpy as np
import pandas as pd
batchSize = 128
eta = 0.8
tasks = 30
imageSize = 32**2
groups = 3
tasksPerGroup = 10
trainDatapoints = 10000
w = np.zeros([imageSize, groups * tasksPerGroup])
toyIndex = 0
for toyLoop in range(groups):
m = np.ones([imageSize]) * np.random.randn(imageSize)
for taskLoop in range(tasksPerGroup):
w[:, toyIndex] = m * 0.1 * np.random.randn(1)
toyIndex += 1
xRand = np.random.normal(0, 0.5, (trainDatapoints, imageSize))
taskLabels = np.matmul(xRand, w) + np.random.normal(0,0.5,(trainDatapoints, groups * tasksPerGroup))
DF = np.concatenate((xRand, taskLabels), axis=1)
trainDF = pd.DataFrame(DF[:trainDatapoints, ])
# define graph variables
x = tf.placeholder(tf.float32, [None, imageSize])
W = tf.Variable(tf.zeros([imageSize, tasks]))
b = tf.Variable(tf.zeros([tasks]))
ystar = tf.placeholder(tf.float32, [None, tasks])
ymask = tf.placeholder(tf.float32, [tasks, tasks])
dataLength = tf.cast(tf.shape(ystar)[0],dtype=tf.float32)
y1 = tf.matmul(x, W) + b
y2 = tf.matmul(y1,ymask)
dist = tf.norm(ystar-y2,axis=0)
mse = tf.reciprocal(dataLength) * tf.reduce_mean(tf.square(dist))
grads = tf.gradients(dist, [y2])
trainStep = tf.train.GradientDescentOptimizer(eta).minimize(mse)
# build graph
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
randTask = np.random.randint(0, 9)
ymaskIn = np.zeros([tasks, tasks])
ymaskIn[randTask, randTask] = 1
batch = trainDF.sample(batchSize)
batch_xs = batch.iloc[:, :imageSize]
batch_ys = np.zeros([batchSize, tasks])
batch_ys[:, randTask] = batch.iloc[:, imageSize + randTask]
gradOut = sess.run(grads, feed_dict={x: batch_xs, ystar: batch_ys, ymask: ymaskIn})
sess.run(trainStep, feed_dict={x: batch_xs, ystar: batch_ys, ymask:ymaskIn})
【问题讨论】:
-
请包含一个独立的 sn-p 以重现该问题(即使用
y1、y2和ystar的常量)。 -
感谢您的快速回复,因此重现的不仅仅是常量,已在问题中添加了一些代码
标签: tensorflow