Tensorflow scatter_update和backpropagation

时间:2016-10-20 20:25:54

标签: tensorflow

我正在尝试运行神经网络来计算结构化对象(如图形)的矢量嵌入。因此,我将为每个图形创建一个嵌入式表示,我将其存储在变量U中,用于训练集中的所有图形。

我面临的问题是我需要在计算渐变之前对U进行更新/分配。 Scatter_update打破了反向传播所需的自动区分。由于算法中需要手动更新,我无法使用tf.nn.embedding_lookup

有没有这样做?我在这里附上了代码。

# Create Hyperparameters
d = 3 
T = 3 
phases = 500 
max_nodes = gtrain.max_nodes
num_classes = gtrain.num_classes
num_graphs = gtrain.graphs.shape[0]

Ug       = np.empty(T, dtype='object')
int_wx1  = np.empty(T, dtype='object')
int_l    = np.empty(T, dtype='object')
int_wl1  = np.empty(T, dtype='object')
int_relu = np.empty(T, dtype='object')

sess = tf.InteractiveSession()

# Feed dictionary
x = tf.placeholder(tf.float32, shape=[max_nodes])
gidx = tf.placeholder(tf.int32, shape=[])
adj = tf.placeholder(tf.float32, shape=[max_nodes, max_nodes])
y_ = tf.placeholder(tf.float32, shape=[num_classes])

# Node Representation
U = tf.Variable(tf.zeros([num_graphs, max_nodes, d]))

w1 = tf.Variable(tf.truncated_normal(shape=[d],stddev=0.1))
w2 = tf.Variable(tf.truncated_normal(shape=[d, d],stddev=0.1))

u = tf.Variable(tf.truncated_normal(shape=[num_classes, d],stddev=0.1))

sess.run(tf.initialize_all_variables())

# Debug Calls
# x = tf.Print(x,[x],"Node labels: ")

for t in range(T):
    Ug[t] = tf.gather(U, gidx)
    int_wx1[t] = tf.matmul(tf.reshape(x, shape=[max_nodes, 1]), tf.reshape(w1, shape=[1, d]))

    int_l[t] = tf.matmul(adj, Ug[t])
    int_wl1[t] = tf.matmul(int_l[t], w2) 
    int_relu[t] = tf.nn.relu(int_wx1[t] + int_wl1[t])

    U_update = tf.scatter_update(U, gidx, int_relu[t])

# Debug Calls
#y_ = tf.Print(y_,[y_],"Labels: ")
#w1 = tf.Print(w1,[w1],"Weights 1: ")
#w2 = tf.Print(w2,[w2],"Weights 2: ")

Uwg = tf.gather(U_update, gidx)

wg = tf.reduce_sum(Uwg, 0)
relug = tf.nn.relu(wg)
#y1 = tf.Print(y1,[y1],"y1: ")
#matmul1 = tf.Print(matmul1,[matmul1],"Matmul1: ")

matmul = tf.matmul(u, tf.reshape(relug, shape=[d,1]))
#matmul = tf.Print(matmul,[matmul],"Matmul: ")

y = tf.reshape(tf.nn.softmax(matmul), shape=[num_classes])
#y = tf.Print(y,[y],"Output: ")

entropy = -tf.reduce_sum(y_ * tf.log(tf.clip_by_value(y,1e-10,1.0)))
#entropy = tf.Print(entropy,[entropy],"Entropy: ")

cross_entropy = tf.reduce_mean(entropy)
#cross_entropy = tf.Print(cross_entropy,[cross_entropy],"Cross Entropy: ")

train_step = tf.train.GradientDescentOptimizer(1e-4).minimize(cross_entropy)

for i in range(phases):
    batch = gtrain.next_batch()
    train_step.run(feed_dict={x: batch[0], gidx: batch[1], adj: batch[2], y_: batch[3]})

0 个答案:

没有答案