Tensorflow:在迷你批次上训练,快速然后慢速训练

时间:2017-12-10 18:45:10

标签: tensorflow mini-batch

我是tensorflow的初学者,我正在尝试使用“迷你批次”训练模型。为此,我创建了一个生成器并迭代它。我遇到的问题是,在纪元开始时,火车似乎很快(每秒多批次)然后火车减速(每秒1批)所以我想知道我的代码在哪里错了但我没有找到问题。

def prepare_data(filename):
    '''load file which give path and label for the data'''
    f = open(filename, 'r')

    data = [line.split() for line in f]
    feat =[]
    label=[]
    for l in data:
        feat.append(l[0])
        label.append(l[1])
    n_samples = len(feat)
    shuf = list(range(n_samples))
    random.shuffle(shuf)

    count = Counter(label)
    print(count)
    feature = [feat[i] for i in shuf]
    label = np.array(label, dtype=np.int)
    return feature, label[shuf]


def get_specgrams(paths, nsamples=16000):
    '''
    Given list of paths, return specgrams.
    '''

    # read the wav files
    wavs = [wavfile.read(x)[1] for x in paths]

    # zero pad the shorter samples and cut off the long ones.
    data = [] 

    for wav in wavs:
        if wav.size < 16000:
            d = np.pad(wav, (nsamples - wav.size, 0), mode='constant')
        else:
            d = wav[0:nsamples]
        data.append(d)

    # get the specgram
    #specgram = [signal.spectrogram(d, nperseg=256, noverlap=128)[2] for d in data]
    #specgram = [s.reshape(129, 124, -1) for s in specgram]

    return np.asarray(data)

def get_specgram(path, nsamples=16000):
    '''
    Given path, return specgrams.
    '''
    # read the wav files
    wav = wavfile.read(path)[1]

    # zero pad the shorter samples and cut off the long ones.


    if wav.size < 16000:
        d = np.pad(wav, (nsamples - wav.size, 0), mode='constant')
    else:
        d = wav[0:nsamples]

    # get the specgram
    #specgram = [signal.spectrogram(d, nperseg=256, noverlap=128)[2] for d in data]
    #specgram = [s.reshape(129, 124, -1) for s in specgram]

    return d
# multci classification binary labels
def one_hot_encode(labels, n_unique_labels=31):
    n_labels = len(labels)
    #print('number of unique labels:', n_unique_labels)
    one_hot_encode = np.zeros((n_labels,n_unique_labels))
    one_hot_encode[np.arange(n_labels), labels] = 1
    return np.array(one_hot_encode, dtype=np.int)
#create_path_file('train/audio/')

def model(tr_features, tr_labels, ts_features, ts_labels):

    # remove gpu device error
    config = tf.ConfigProto(allow_soft_placement = True)

    # parameters
    BATCH_SIZE = 4
    number_loop = math.ceil(len(tr_features)/BATCH_SIZE)
    training_epochs = 10
    n_dim = 16000
    n_classes = 31 #len(np.unique(ts_labels))

    n_hidden_units_one = 280 
    n_hidden_units_two = 300
    sd = 1 / np.sqrt(n_dim)
    learning_rate = 0.1

    # get test data
    ts_features, ts_labels = get_data(ts_features, ts_labels)

    # Model
    X = tf.placeholder(tf.float32,[None,n_dim])
    Y = tf.placeholder(tf.float32,[None,n_classes])

    W_1 = tf.Variable(tf.random_normal([n_dim,n_hidden_units_one], mean = 0, stddev=sd))
    b_1 = tf.Variable(tf.random_normal([n_hidden_units_one], mean = 0, stddev=sd))
    h_1 = tf.nn.tanh(tf.matmul(X,W_1) + b_1)


    W_2 = tf.Variable(tf.random_normal([n_hidden_units_one,n_hidden_units_two], mean = 0, stddev=sd))
    b_2 = tf.Variable(tf.random_normal([n_hidden_units_two], mean = 0, stddev=sd))
    h_2 = tf.nn.sigmoid(tf.matmul(h_1,W_2) + b_2)


    W = tf.Variable(tf.random_normal([n_hidden_units_two,n_classes], mean = 0, stddev=sd))
    b = tf.Variable(tf.random_normal([n_classes], mean = 0, stddev=sd))
    y_ = tf.nn.softmax(tf.matmul(h_2,W) + b)

    init = tf.initialize_all_variables()
    # function and optimizers
    cost_function = -tf.reduce_sum(Y * tf.log(y_))
    optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost_function)

    correct_prediction = tf.equal(tf.argmax(y_,1), tf.argmax(Y,1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    # train loop

    cost_history = np.empty(shape=[1],dtype=float)
    y_true, y_pred = None, None
    with tf.Session(config = config) as sess:
        sess.run(init)
        for epoch in range(training_epochs):
            print(' ## Epoch n°', epoch+1 )
            batch = batch_generator(BATCH_SIZE, tr_features, tr_labels)
            acc_total = 0.0
            for cpt, (train_features_batch, train_labels_batch) in enumerate(batch):
                _,cost = sess.run([optimizer,cost_function],feed_dict={X:train_features_batch,Y:train_labels_batch})
                cost_history = np.append(cost_history,cost)

                correct_prediction = tf.equal(tf.argmax(y_,1), tf.argmax(Y,1))
                accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
                acc = accuracy.eval(feed_dict={X:train_features_batch,Y:train_labels_batch})
                acc_total = (acc_total*cpt + acc)/(cpt+1)
                print('Train accuracy : ', acc_total, '[',str(cpt+1), '/',str(number_loop), ']' ,flush=True, end='\r') 
                clear_output()

            print('Train accuracy : ', acc_total)   
            y_pred = sess.run(tf.argmax(y_,1),feed_dict={X: ts_features})
            y_true = sess.run(tf.argmax(ts_labels,1))
            print('Test accuracy: ', round(sess.run(accuracy, feed_dict={X: ts_features, Y: ts_labels}) , 3))

    fig = plt.figure(figsize=(10,8))
    plt.plot(cost_history)
    plt.axis([0,training_epochs,0,np.max(cost_history)])
    plt.show()

    p,r,f,s = precision_recall_fscore_support(y_true, y_pred, average='micro')
    print("F-Score:", round(f,3))


def batch_generator(batch_size, feat_path, labels):
    n_sample = len(feat_path)
    ite = math.ceil(n_sample/batch_size)

    for i in range(0, ite):
        if i == ite-1:
            label = one_hot_encode(labels[-batch_size:])
            feat = get_specgrams(feat_path[-batch_size:])
            yield (feat, label)
        else:
            label = one_hot_encode(labels[i*batch_size:i*batch_size+batch_size])
            feat = get_specgrams(feat_path[i*batch_size:i*batch_size+batch_size])
            yield (feat, label)

def get_data(feat_path, labels):
    feat = get_specgrams(feat_path)
    label = one_hot_encode(labels)
    return feat, label

def __main__():
    print('## Load data and shuffle')
    feat_path, labels = prepare_data('data_labelised2.txt')
    idx = int(len(labels)*0.8)
    print("## Create Model")
    model(feat_path[0:idx], labels[0:idx], feat_path[idx+1:], labels[idx+1:])


with tf.device('/gpu:0'):
    __main__()

0 个答案:

没有答案
相关问题