我正在学习GAN,并希望在R中编写一个程序,使用来自MNIST数据集的数据训练GAN模型生成手写数字。
以下是我的代码:
datasets <- tf$contrib$learn$datasets
mnist <- datasets$mnist$read_data_sets("MNIST-data")
discriminator <- function(images, reuse = FALSE){
if (reuse) {
tf$get_variable_scope()$reuse_variables()
}
# First convolutional layer
d_w1 = tf$get_variable(name = "d_w1", dtype = tf$float32, shape = list(5L, 5L, 1L, 32L), initializer = tf$truncated_normal_initializer(stddev = 0.2))
d_b1 = tf$get_variable(name = "d_b1", dtype = tf$float32, shape = list(32L), initializer = tf$truncated_normal_initializer(stddev = 0.2))
d1 = tf$nn$conv2d(input=images, filter=d_w1, strides=list(1L, 1L, 1L, 1L), padding='SAME')
d1 = d1 + d_b1
d1 = tf$nn$relu(d1)
d1 = tf$nn$avg_pool(d1, ksize=list(1L, 2L, 2L, 1L), strides=list(1L, 2L, 2L, 1L), padding='SAME')
# Second convolutional layer
d_w2 = tf$get_variable(name = "d_w2", shape = list(5L, 5L, 32L, 64L), initializer=tf$truncated_normal_initializer(stddev=0.02))
d_b2 = tf$get_variable(name = "d_b2", shape = list(64L), initializer=tf$constant_initializer(0))
d2 = tf$nn$conv2d(input=d1, filter=d_w2, strides=list(1L, 1L, 1L, 1L), padding='SAME')
d2 = d2 + d_b2
d2 = tf$nn$relu(d2)
d2 = tf$nn$avg_pool(d2, ksize=list(1L, 2L, 2L, 1L), strides=list(1L, 2L, 2L, 1L), padding='SAME')
# First fully connected layer
d_w3 = tf$get_variable(name = "d_w3", shape = list(7L * 7L * 128L, 1024L), initializer=tf$truncated_normal_initializer(stddev=0.02))
d_b3 = tf$get_variable(name = "d_b3", shape = list(1024L), initializer=tf$constant_initializer(0))
d3 = tf$reshape(d2, shape = list(-1L, as.integer(7*7*64)))
d3 = tf$matmul(d3, d_w3)
d3 = d3 + d_b3
d3 = tf$nn$relu(d3)
# Second fully connected layer
d_w4 = tf$get_variable('d_w4', shape = list(1024L, 1L), initializer=tf$truncated_normal_initializer(stddev=0.02))
d_b4 = tf$get_variable('d_b4', shape = list(1L), initializer=tf$constant_initializer(0))
d4 = tf$matmul(d3, d_w4) + d_b4
d4 = tf$sigmoid(d4)
# d4 contains unscaled values
return(d4)
}
# Define the generator network
generator <- function(batch_size, z_dim, reuse = FALSE){
if (reuse){
tf$get_variable_scope()$reuse_variables()
}
z = tf$random_normal(shape = list(batch_size, z_dim), mean=0, stddev=1, name='z')
g_w1 = tf$get_variable('g_w1', shape = list(z_dim, 3136L), dtype=tf$float32, initializer=tf$truncated_normal_initializer(stddev=0.02))
g_b1 = tf$get_variable('g_b1', shape = list(3136L), initializer=tf$truncated_normal_initializer(stddev=0.02))
g1 = tf$matmul(z, g_w1) + g_b1
g1 = tf$reshape(g1, list(-1L, 56L, 56L, 1L))
g1 = tf$nn$relu(g1)
# Generate 50 features
g_w2 = tf$get_variable('g_w2', shape = list(3L, 3L, 1L, as.integer(z_dim/2)), dtype=tf$float32, initializer=tf$truncated_normal_initializer(stddev=0.02))
g_b2 = tf$get_variable('g_b2', shape = list(as.integer(z_dim/2)), initializer=tf$truncated_normal_initializer(stddev=0.02))
g2 = tf$nn$conv2d(g1, g_w2, strides=list(1L, 2L, 2L, 1L), padding='SAME')
g2 = g2 + g_b2
g2 = tf$nn$relu(g2)
g2 = tf$image$resize_images(g2, list(56L, 56L))
# Generate 25 features
g_w3 = tf$get_variable('g_w3', shape = list(3L, 3L, as.integer(z_dim/2), as.integer(z_dim/4)), dtype=tf$float32, initializer=tf$truncated_normal_initializer(stddev=0.02))
g_b3 = tf$get_variable('g_b3', shape = list(as.integer(z_dim/4)), initializer=tf$truncated_normal_initializer(stddev=0.02))
g3 = tf$nn$conv2d(g2, g_w3, strides=list(1L, 2L, 2L, 1L), padding='SAME')
g3 = g3 + g_b3
g3 = tf$nn$relu(g3)
g3 = tf$image$resize_images(g3, list(56L, 56L))
# Final convolution with one output channel
g_w4 = tf$get_variable('g_w4', shape = list(1L, 1L, as.integer(z_dim/4), 1L), dtype=tf$float32, initializer=tf$truncated_normal_initializer(stddev=0.02))
g_b4 = tf$get_variable('g_b4', shape = list(1L), initializer=tf$truncated_normal_initializer(stddev=0.02))
g4 = tf$nn$conv2d(g3, g_w4, strides=list(1L, 2L, 2L, 1L), padding='SAME')
g4 = g4 + g_b4
g4 = tf$sigmoid(g4)
# Dimensions of g4: batch_size x 28 x 28 x 1
return(g4)
}
# Declare the batch size and the dimension of the generate output
z_dimensions = 100L
batch_size = 50L
# Create a placeholder for input
x_placeholder = tf$placeholder(tf$float32, shape = list(NULL,28L,28L,1L), name='x_placeholder')
# Gz holds the generated image
Gz = generator(batch_size, z_dimensions)
我收到了这个错误:
Error in py_call_impl(callable, dots$args, dots$keywords) :
ValueError: Variable g_w1 does not exist, or was not created with tf.get_variable(). Did you mean to set reuse=None in VarScope?
我试图修正错误:
1)我使用tf$get_variable()
而不是tf$Variable()
。错误消失了,但根据这个
discussion,我应该使用tf$get_variable()
而不是tf$Variable
,因为只有tf$get_variable()
创建的变量才会被变量范围识别。
2)我试图将Gz
的赋值包含在这样的范围内:
with(tf$variable_scope(tf$get_variable_scope(), reuse = FALSE),{
Gz = generator(batch_size, z_dimensions)
})
但仍然存在同样的错误。
我很感激有关如何解决此错误的任何建议。非常感谢你!