简体   繁体   English

尝试以频谱图的形式对音频进行分类时,收到错误消息“ValueError:没有为任何变量提供渐变:...”

[英]When trying to classify audio in the form of spectograms getting an error message “ValueError: No gradients provided for any variable:…”

Im trying to classify spectograms of shape 40, 501 and there are 15 classes.我试图对形状 40、501 的频谱图进行分类,并且有 15 个类别。

import numpy as np
import tensorflow as tf
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Dense, Flatten


X_test = np.load("X_test.npy")
y_test = np.load("y_test.npy")
X_train = np.load("X_train.npy")
y_train = np.load("y_train.npy")
X_train = X_train[..., np.newaxis]
y_train = to_categorical(y_train, 15)

model = tf.keras.models.Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), input_shape=(40, 501, 1), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(15, activation='softmax'))

model.summary()
model.compile()
model.fit(X_train, y_train, epochs=5)

Training data consists of 4500 spectograms (the data is preprocessed and I havent touched it. When i try the model.fit Im getting an error message:训练数据由 4500 个频谱图组成(数据已经过预处理,我没有接触过它。当我尝试model.fit时,我收到一条错误消息:

Error:错误:

ValueError: No gradients provided for any variable: ['conv2d/kernel:0', 'conv2d/bias:0', 'conv2d_1/kernel:0', 'conv2d_1/bias:0', 'dense/kernel:0', 'dense/bias:0'].

This an issue with how you coded the data generation, probably not in Tensorflow.这是您如何编码数据生成的问题,可能不在 Tensorflow 中。 It should return a tuple or a dict of tuples in dataloader.它应该在数据加载器中返回一个元组或一个元组字典。

To resolve this you must modify the data generator function as:要解决此问题,您必须将数据生成器 function 修改为:

yield([array(Ximages),array(XSeq)],array()) 

instead of:代替:

yield [[array(Ximages), array(XSeq)], array(y)]

Sample structure:样本结构:

# data generator, intended to be used in a call to model.fit_generator()
def data_generator(descriptions, features, n_step, max_sequence):
    # loop until we finish training
    while 1:
        # loop over photo identifiers in the dataset
        for i in range(0, len(descriptions), n_step):
            Ximages, XSeq, y = list(), list(),list()
            for j in range(i, min(len(descriptions), i+n_step)):
                image = features[j]
                # retrieve text input
                desc = descriptions[j]
                # generate input-output pairs
                in_img, in_seq, out_word = preprocess_data([desc], [image], max_sequence)
                for k in range(len(in_img)):
                    Ximages.append(in_img[k])
                    XSeq.append(in_seq[k])
                    y.append(out_word[k])

       ### —-> CHANGE THIS TO FIT YOUR DATA: yield this batch of samples to the model
            #yield [[array(Ximages), array(XSeq)], array(y)]
yield([array(Ximages),array(XSeq)],array()) 

#Create the network 
image_model = Sequential()
image_model.add(Conv2D(16, (3, 3), padding='valid', activation='relu', input_shape=(256, 256, 3,)))
image_model.add(Conv2D(16, (3,3), activation='relu', padding='same', strides=2))
image_model.add(Conv2D(32, (3,3), activation='relu', padding='same'))
image_model.add(Conv2D(32, (3,3), activation='relu', padding='same', strides=2))
image_model.add(Conv2D(64, (3,3), activation='relu', padding='same'))
image_model.add(Conv2D(64, (3,3), activation='relu', padding='same', strides=2))
image_model.add(Conv2D(128, (3,3), activation='relu', padding='same'))

...
### —> data generator is called on model fit 
model.fit_generator(data_generator(texts, train_features, 1, max_sequence), steps_per_epoch=steps, epochs=1, callbacks=callbacks_list, verbose=1)
model.save(mydrive + '/output/weights.hdf5')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TensorFlow 错误:ValueError:没有为任何变量提供梯度 - TensorFlow Error: ValueError: No gradients provided for any variable 我收到一个 ValueError:没有为任何变量提供渐变 - I'm getting a ValueError: No gradients provided for any variable 为什么在 keras 中使用 train_step() 时会收到错误“ValueError:没有为任何变量提供梯度:”? - Why am I getting the error "ValueError: No gradients provided for any variable:" while using train_step() in keras? 在培训期间,我收到以下错误。 “ ValueError:没有为任何变量提供梯度” - Whie training I am getting the following error. “ ValueError: No gradients provided for any variable” TensorFlow/Keras 中的错误:ValueError:没有为任何变量提供梯度 - Error in TensorFlow/Keras: ValueError: No gradients provided for any variable 自定义损失 Function 错误:ValueError:没有为任何变量提供梯度 - Custom Loss Function Error: ValueError: No gradients provided for any variable ValueError:计算损失时没有为任何变量提供梯度 - ValueError: No gradients provided for any variable when calculating loss 自定义损失 Function 错误:ValueError:没有为任何变量提供梯度(TensorFlow) - Custom Loss Function Error: ValueError: No gradients provided for any variable (TensorFlow) ValueError:没有为任何变量提供渐变:['Variable:0'] - ValueError: No gradients provided for any variable: ['Variable:0'] ValueError:在构建 GAN 时,没有为 TensorFlow 中的任何变量提供梯度 - ValueError: No gradients provided for any variable in TensorFlow when building a GAN
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM