Keras:没有为任何变量提供渐变 [英] Keras: No gradients provided for any variable

查看:41
本文介绍了Keras:没有为任何变量提供渐变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究基于GAN的问题,但是在看不到渐变的情况下我遇到了这个问题.我希望有人能在这里帮助我.我有两个主要的代码段,它们也许可以帮助您确定问题.第一段代码将数据加载到变量中.

I am working on a GAN based problem and I am getting this issue where the gradients are not visible. I was hoping if someone can help me here. I have two main piece of codes that might be able to help you identify the issue. First piece of code loads the data in the variables.

training_images = []
target_images = []

for image in images:
    a_img = cv2.resize(cv2.imread( os.path.join(Augment_img_dir, image),0), (128,128))/255
    
    lsri_img = cv2.resize(cv2.imread( os.path.join(LSRI_img_dir, image),0), (128,128), cv2.INTER_NEAREST)/255
    
    hsri_img = cv2.resize(cv2.imread( os.path.join(HSRI_img_dir, image),0), (128,128))/255
    
    img_train = [a_img, lsri_img]
    img_train = np.asarray(img_train)
    img_train = np.moveaxis(img_train,0, -1)
    
    training_images.append(img_train)
    
    target_images.append(hsri_img)

training_images = np.asarray(training_images)

target_images = np.asarray(target_images)

train_imgs, test_imgs, train_targets, test_targets = train_test_split(training_images, target_images, 
                                                                     test_size=.20, random_state=42)

batch_size = 8
train_img_batch = []
target_img_batch = []
len_imgs = len(train_imgs)
start = 0
temp_train = []
temp_target = []
for i in range(len_imgs+1):
    if i%batch_size == 0 and i>0:
        train_img_batch.append(np.asarray(temp_train))
        target_img_batch.append(np.asarray(temp_target))
        temp_train = []
        temp_target = []
    if i != len_imgs:
        temp_train.append(train_imgs[i])
        temp_target.append(train_targets[i])

train_img_batch = np.asarray(train_img_batch)
target_img_batch = np.asarray(target_img_batch)

第二段代码是我遇到错误的实际火车功能.

The second piece of code is the actual train function where I am getting the error.

batch_size = 8
lr = 0.0001
G_optimizer = Adam(learning_rate=lr)
n_epoch  = 100
iterations = 13500
def train():
    G = Generator((128,128,2)).generator()
    D = Discriminator((128,128,1)).discriminator()
    
    g_optimizer_init = tf.optimizers.Adam(learning_rate=lr)
    g_optimizer = tf.optimizers.Adam(learning_rate=lr)
    d_optimizer = tf.optimizers.Adam(learning_rate=lr)
    mse_loss = keras.losses.MeanSquaredError()
    n_step_epoch = round(n_epoch // batch_size)    
    
    G_Loss_file = open("g_loss.txt",'w')
    
    
    for epoch in range(n_epoch):
        step_time = time.time()
        for step, lr_patchs in enumerate(tf.data.Dataset.from_tensor_slices(train_img_batch)):
            if lr_patchs.shape[0] != batch_size: # if the remaining data in this epoch < batch_size
                break
            
            hr_patchs = target_img_batch[step]
            with tf.GradientTape() as tape:
                #tape.watch(G.trainable_weights)
                fake_hr_patchs = G(lr_patchs)
                fake_hr_patchs = np.reshape(fake_hr_patchs, (8,128,128))
                mse_loss = mse_loss(fake_hr_patchs, hr_patchs)
                
            grad = tape.gradient(mse_loss, G.trainable_variables)
            g_optimizer_init.apply_gradients(zip(grad, G.trainable_variables))

        print("Epoch: [{}/{}], time: {:.2f}s, mse: {:.2f} ".format(
                epoch, n_epoch, time.time() - step_time, mse_loss))
            
        G_Loss_file.write("Epoch: [{}/{}], time: {:.2f}s, mse: {:.2f} \n".format(
            epoch, n_epoch, time.time() - step_time, mse_loss))
    G_Loss_file.close()
    
    ## adversarial learning (G, D)
    Loss_file = open("loss.txt",'w')
    n_step_epoch = round(n_epoch // batch_size)
    for epoch in range(n_epoch):
        step_time = time.time()
        for step, lr_patchs in enumerate(train_img_batch):
            if lr_patchs.shape[0] != batch_size: # if the remaining data in this epoch < batch_size
                break
                
            hr_patchs = target_img_batch[step]
            with tf.GradientTape(persistent=True) as tape:
                fake_patchs = G(lr_patchs)
                fake_patchs = np.reshape(fake_patchs, (8,128,128))
                logits_fake = D(fake_patchs)
                logits_real = D(hr_patchs)
                
                
                #d_Loss_int = Intensity_Loss(logits_fake, logits_real)

                d_loss1 = tl.cost.sigmoid_cross_entropy(logits_fake, tf.zeros_like(logits_fake))
                
                d_loss = -np.log(d_loss1) #+ 0.1*d_Loss_int
                
                g_gan_loss = 1e-3 * tl.cost.sigmoid_cross_entropy(logits_fake, tf.ones_like(logits_fake))
                mse_loss = tl.cost.mean_squared_error(fake_patchs, hr_patchs, is_mean=True)
                
                
                g_loss = mse_loss + g_gan_loss
            
            grad = tape.gradient(g_loss, G.trainable_weights)
            #print(grad, len(G.trainable_weights))
            g_optimizer.apply_gradients(zip(grad, G.trainable_weights))
            grad = tape.gradient(d_loss, D.trainable_weights)
            d_optimizer.apply_gradients(zip(grad, D.trainable_weights))
        
        print("Epoch: [{}/{}], time: {:.3f}s, g_loss(mse:{:.3f},  adv:{:.3f}) d_loss: {:.3f}".format(
            epoch, n_epoch, time.time() - step_time, mse_loss, g_gan_loss, d_loss))
            
        Loss_file.write("Epoch: [{}/{}], time: {:.3f}s, g_loss(mse:{:.3f},  adv:{:.3f}) d_loss: {:.3f}".format(
            epoch, n_epoch, time.time() - step_time, mse_loss, g_gan_loss, d_loss))
        
        if epoch!=0 and ((epoch%10 == 0) or (epoch == n_epoch-1)):
            
            G.save_weights("Gan_Weights/g_training_20_noise_no_contrast.h5")
            D.save_weights("Gan_Weights/d_training_20_noise_no_contrast.h5")
        
    Loss_file.close()

出现错误的行如下:

g_optimizer_init.apply_gradients(zip(grad, G.trainable_variables))

,错误如下:

No gradients provided for any variable: ['conv2d_392/kernel:0', 'conv2d_392/bias:0', 'batch_normalization_312/gamma:0', 'batch_normalization_312/beta:0', 'conv2d_393/kernel:0', 'conv2d_393/bias:0', 'batch_normalization_313/gamma:0', 'batch_normalization_313/beta:0', 'conv2d_394/kernel:0', 'conv2d_394/bias:0', 'batch_normalization_314/gamma:0', 'batch_normalization_314/beta:0', 'conv2d_395/kernel:0', 'conv2d_395/bias:0', 'batch_normalization_315/gamma:0', 'batch_normalization_315/beta:0', 'conv2d_396/kernel:0', 'conv2d_396/bias:0', 'conv2d_397/kernel:0', 'conv2d_397/bias:0', 'batch_normalization_316/gamma:0', 'batch_normalization_316/beta:0', 'conv2d_398/kernel:0', 'conv2d_398/bias:0', 'batch_normalization_317/gamma:0', 'batch_normalization_317/beta:0', 'conv2d_399/kernel:0', 'conv2d_399/bias:0', 'batch_normalization_318/gamma:0', 'batch_normalization_318/beta:0', 'conv2d_400/kernel:0', 'conv2d_400/bias:0', 'batch_normalization_319/gamma:0', 'batch_normalization_

推荐答案

您正在GradientTape范围内的多个位置(例如,在行中)使用numpy操作

You are using numpy operations in several places inside the scope of GradientTape, for example in the line

fake_hr_patchs = np.reshape(fake_hr_patchs, (8,128,128))

tensorflow等效项替换GradientTape范围内的所有numpy函数.例如,上面的行变为:

Replace all numpy functions inside GradientTape's scope with their tensorflow equivalents. For example, the above line becomes:

fake_hr_patchs = tf.reshape(fake_hr_patchs, (8,128,128))

这篇关于Keras:没有为任何变量提供渐变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆