def train_one_epoch(model, param_dict, input_names, output_name, X_shuffled,
                    Y_shuffled):
    batch_size = int(param_dict['batch_size'])
    l = len(X_shuffled)
    train_loss = 0
    train_acc = 0
    for i in range(0, l, batch_size):
        batch_end = min(i + batch_size, l)
        Xs = X_shuffled[i:batch_end]
        Ys = Y_shuffled[i:batch_end]
        batchsize = len(Xs)

        batch = gd.get_batch(Xs, param_dict)
        batch = da.augment_data(batch, param_dict, "train")

        #If single stream model, we have 1 input_name, otherwise 2
        fit_input = make_model_input_dict(input_names, batch)

        # Train on single batch
        history = model.fit(fit_input, {output_name: Ys}, batch_size=batchsize)

        train_loss += float(history.history['loss'][0]) * (batchsize / l)
        train_acc += float(history.history['acc'][0]) * (batchsize / l)

    return model, train_loss, train_acc
Exemple #2
0
def train(model,loss_func,optim,poem_vec_lst7, num_epochs, model_path):
     global BATCH_SIZE,get_batch,idx2Wd,vocab_size,sentence_len
     for epoch in range(start_epoch,start_epoch+num_epochs):
          print('Starting epoch %d / %d' % (epoch+1,start_epoch+num_epochs))
          model.train()  #设置网络模型为train模式
          #每个epoch取epoch_size//batch_size次,分别使idx取0,1,2,...,epoch_size//batch_size-1
          for idx in range(len(poem_vec_lst7)//BATCH_SIZE):
               print(idx*BATCH_SIZE,"/",len(poem_vec_lst7))
               batch_x,batch_y=get_batch(poem_vec_lst7,BATCH_SIZE,idx, 7)#得到batch_x,batch_y
               batch_x=np.array(batch_x)
               batch_y=np.array(batch_y)
               output=model(batch_x,True)#output为模型预测的向量
            
               #将output变成BATCH_SIZE*3(句)*每句sentence_len个字,每个字有vocab_size种选择的向量
               output=torch.reshape(output,(BATCH_SIZE*3*sentence_len,vocab_size))
               #将output变成BATCH_SIZE*3(句)*每句sentence_len个字,每个位置是ground_truth对应字的index
               batch_y=np.reshape(batch_y,BATCH_SIZE*3*sentence_len)
               batch_y=torch.LongTensor(batch_y)
               #output中选出概率最大的字,这个字的index放到wordidx里
               wordidx=torch.argmax(output,dim=1)
               
               
               loss=loss_func(output,batch_y)#output是每个位置预测每个字的概率分布矩阵,batch_y是一维数组,分别是ground_truth中每个位置字的index
               print('loss is %lf'%(loss.item()))
               
               loss.backward()
               optim.step()
               optim.zero_grad()
          #print('epoch '%) 
          #保存模型
          print("save model in epoch %d "%(epoch))
          torch.save(model.state_dict(), model_path + "\\rnn7_epoch_%d.pth"%(epoch))
def validate_one_epoch(model, param_dict, input_names, output_name, X_val,
                       Y_val):
    X_val_data = gd.get_batch(X_val, param_dict)
    X_val_augmented = da.augment_data(X_val_data, param_dict, "val")

    # Evaluate on validation data
    print("Evaluating On Validation Data...")
    fit_input = make_model_input_dict(input_names, X_val_augmented)
    val_loss, val_acc = model.evaluate(fit_input, {output_name: Y_val})
    return val_loss, val_acc
Exemple #4
0
def main():
    # 선형회귀와 같은 파라미터
    # 특징 벡터의 차원 설정
    dimension = 10
    # 비선형 플래그
    nonlinear = False
    # 전체 데이터의 수
    num_of_samples = 1000
    # 노이즈의 진폭
    noise_amplitude = 0.01

    # 하이퍼 파라미터의 설정
    batch_size = 10
    max_epoch = 10000
    learning_rate = 1e-3
    momentum = 0.9  # 이 값을  하면 모멘트법이 됩니다
    regularize_coeff = 0.0  # 이 값을 하면 L2 노름에 의한 법칙이 걸립니다

    # 전체 데이터 구하기
    # NOTE: 여기에서는 미니배치 동작만을 보기 위해서
    #       일단 전체 데이터를 취득하고 나서 배치 단위로 잘라서 반환합니다
    #       그러나, 미니배치법이 필요하게 되는 상황에서는
    #       전체 데이터를 한번에 읽어 들이지 않는 경우가 보통이므로
    #       수 배치분을 읽어 들여 버퍼하고 나서 1배치만 반환해야 합니다
    #       이러한 경우, python의 기능을 살려
    #       순서를 따라 읽어 들이는 제너레이터를 작성하는 것이 유효합니다
    data_train, (X_test, Y_test) = get_all(dimension,
                                           nonlinear,
                                           num_of_samples,
                                           noise_amplitude,
                                           return_coefficient_matrix=False)
    A_test = coeff(X_test)

    # 손실의 이력
    train_losses = []
    test_losses = []
    # 파라미터의 초기값
    D_hat = np.zeros(dimension + 1)
    # 에포크에 대해서의 루프
    for epoch in range(max_epoch):
        print("epoch: {0} / {1}".format(epoch, max_epoch))
        # 배치 단위로 분할
        data_train_batch = get_batch(data_train, batch_size)
        # 1에포크 분 학습
        loss, D_hat = train_epoch(data_train_batch, D_hat, learning_rate,
                                  momentum, regularize_coeff)

        # 손실을 이력에 저장
        train_losses.append(loss)

        # 전형적인 코드에서는 어떤 에포크에 한 번 테스트를 실시하고
        # 도중 경과가 어느 정도의 범화성능을 나타내는지 확인하지만
        # 여기에서는 매회 테스트를 실시하고 있습니다
        Y_hat = A_test.dot(D_hat)
        E = Y_hat - Y_test
        test_loss = E.T.dot(E)
        test_losses.append(test_loss)

    # 실험기록용
    parameters = {
        "linearity": "nonlinear" if nonlinear else "linear",
        "dimension": dimension,
        "num_of_samples": num_of_samples,
        "batch_size": batch_size,
        "max_epoch": max_epoch,
        "learning_rate": learning_rate,
        "momentum": momentum,
        "regularize_coeff": regularize_coeff,
    }
    # 결과 표시
    visualize_result("linear_regression_iterative_solution",
                     A_test[:, 0:2],
                     Y_test,
                     Y_hat,
                     parameters,
                     losses=(train_losses, test_losses))
Exemple #5
0
# model=FastText2(maxlen,embedding_dim,word_dict,num_class)
print('fit...')
# train_data=np.array(train_tensor)
# train_label=to_categorical(np.array(train_label),num_classes=num_class)

#model.fit(train_data,train_label,validation_split=0.1,epochs=10,batch_size=64,callbacks=callback_list,verbose=0)
max_acc = 0.0
for iter in range(iter_num):
    i = 0
    sum_loss = 0.
    sum_acc = 0.
    data_length = len(train_tensor)
    batch_nums = (data_length // batch_size) - 1

    while i < batch_nums:
        batch_data, batch_label = get_batch(batch_size, i, train_tensor,
                                            train_label)
        batch_data = np.array(batch_data)
        batch_label = np.array(batch_label)
        batch_label = to_categorical(batch_label, num_classes=num_class)
        batch_train, batch_test, batch_train_label, batch_test_label = train_test_split(
            batch_data, batch_label, test_size=0.1, random_state=22)

        batch_train = np.array(batch_train)
        batch_test = np.array(batch_test)
        model.train_on_batch(batch_train, batch_train_label)
        i += 1
        print('iter : %d, i :%d' % (iter, i))
        loss, acc = model.evaluate(batch_test,
                                   batch_test_label,
                                   batch_size=batch_size)
        print('Train loss: %f, Train accuracy: %f ' % (loss, acc))
model = tu.make_network_model(param_dict,stream,None)

weights_pre = model.get_weights()
model = tu.load_weights(model, param_dict, stream, False, "test")
weights_after = model.get_weights()
weight_names = [weight.name for layer in model.layers for weight in layer.weights]
tu.check_weights(weights_pre, weights_after, weight_names)


# If the frame at second 1 is used (time of shot in B3SD dataset), then
#  we only get that particular frame for each video.

if sec1_frame:
    print("1sec_frame set, so choosing that frame for each video")
    result_file.write("1sec_frame set, so choosing that frame for each video\n")
    X = gd.get_batch(X, param_dict)
    augmented_X = da.augment_data(X, param_dict, "test")


    fit_input = tu.make_model_input_dict(input_names, augmented_X)
    predictions_categorical = model.predict(fit_input, verbose=1)
    
    print(predictions_categorical.shape)
    predictions = np.asarray([np.argmax(pred) for pred in predictions_categorical])
    print(predictions.shape)

    acc, correct_predictions = tpu.calculate_accuracy(predictions, Y, len(predictions))

# If sec1_frame is false, then we select x frames from each video for a 
#   more averaged guess for each video.
else:
Exemple #7
0
def main():
    # 선형회귀와 같은 파라미터
    # 특징 벡터의 차원의 설정
    dimension = 10
    # 비선형 플래그
    nonlinear = True
    # 전체 데이터 수
    num_of_samples = 1000
    # 노이즈 진폭
    noise_amplitude = 0.01

    # 선형회귀와 공통의 하이퍼 파라미터
    batch_size = 10
    max_epoch = 10000
    learning_rate = 1e-3
    momentum = 0.0
    regularize_coeff = 0.0

    # 뉴럴 네트워크 특유의 하이퍼 파라미터
    # 중간층의 유닛수(채널 수)
    # NOTE: 여기에서는 1층만으로 하지만
    #       리스트에 값을 붙여 나감으로써 층을 추가할 수 있습니다
    #       단지, 메모리 사용량에는 주의하세요!
    num_units = [
        50,
        100,
    ]

    # 전체 데이터 구하기
    data_train, (X_test, Y_test) = get_all(dimension,
                                           nonlinear,
                                           num_of_samples,
                                           noise_amplitude,
                                           return_coefficient_matrix=False)

    # 손실의 이력
    train_losses = []
    test_losses = []
    # 파라미터의 초기값
    W_hat = init_weights(num_units, dimension)
    for epoch in range(max_epoch):
        print("epoch: {0}/{1}".format(epoch, max_epoch))
        # 배치 단위로 분할
        data_train_batch = get_batch(data_train, batch_size)
        # 1에포크 분 학습
        train_loss, W_hat = train_epoch(data_train_batch, W_hat, learning_rate,
                                        momentum, regularize_coeff)

        # 결과를 이력에 저장
        train_losses.append(train_loss)

        # 테스트 데이터로의 피팅
        Y_hat = forward(X_test, W_hat)[-1][:, 0]
        E = Y_hat - Y_test
        test_loss = E.T.dot(E)
        test_losses.append(test_loss)

    # 실험 기록용
    parameters = {
        "linearity": "nonlinear" if nonlinear else "linear",
        "dimension": dimension,
        "num_of_samples": num_of_samples,
        "batch_size": batch_size,
        "max_epoch": max_epoch,
        "learning_rate": learning_rate,
        "momentum": momentum,
        "regularize_coeff": regularize_coeff,
        "num_units": num_units,
    }
    # 결과 표시
    visualize_result("neural_network",
                     X_test[:, 0:2],
                     Y_test,
                     Y_hat,
                     parameters,
                     losses=(train_losses, test_losses))
model_part1 = tu.make_network_model(param_dict_part1,stream_part1,None)


weights_pre = model_part1.get_weights()
model_part1 = tu.load_weights(model_part1, param_dict_part1, stream_part1, True, "test")
weights_after = model_part1.get_weights()
weight_names = [weight.name for layer in model_part1.layers for weight in layer.weights]
tu.check_weights(weights_pre, weights_after, weight_names)


# If the frame at second 1 is used (time of shot in B3SD dataset), then
#  we only get that particular frame for each video.

print("1sec_frame set, so choosing that frame for each video")
result_file.write("1sec_frame set, so choosing that frame for each video\n")
X2_data = gd.get_batch(X2, param_dict_part1)
augmented_X2 = da.augment_data(X2_data, param_dict_part1, "test")


fit_input2 = tu.make_model_input_dict(input_part1, augmented_X2)
predictions_categorical2 = model_part1.predict(fit_input2, verbose=1)
    
print(predictions_categorical2.shape)
predictions2 = np.asarray([np.argmax(pred) for pred in predictions_categorical2])
print(predictions2.shape)
acc2, correct_predictions2 = tpu.calculate_accuracy(predictions2, Y2, len(predictions2))


correct=0
ones = 0
actual_shots=0
Exemple #9
0
CAPACITY = 1000  #每批次最大容量
MAX_STEP = 100000  # 训练总批次
VAL_SPACE = 100  #训练几批,测试一次
learning_rate = 0.0001  # 学习率

# 用这个程序,请改路径
train_dir = '/home/emin/Temp/Tensorflow/data/cifar-10/train/'
test_dir = '/home/emin/Temp/Tensorflow/data/cifar-10/test/'
logs_train_dir = '/home/emin/Temp/Tensorflow/Me/cifar_me/train/'
logs_val_dir = '/home/emin/Temp/Tensorflow/Me/cifar_me/val/'
arra = [0] * (int(MAX_STEP / VAL_SPACE))  #储存训练准确度
brra = [0] * (int(MAX_STEP / VAL_SPACE))  #储存测试准确度
train, train_label = get_data.get_files(train_dir)  #读训练集
val, val_label = get_data.get_files(test_dir)  #读测试集
train_batch, train_label_batch = get_data.get_batch(train, train_label, IMG_W,
                                                    IMG_H, BATCH_SIZE,
                                                    CAPACITY)
val_batch, val_label_batch = get_data.get_batch(val, val_label, IMG_W, IMG_H,
                                                BATCH_SIZE, CAPACITY)
#生成批次
x = tf.placeholder(tf.float32, shape=[BATCH_SIZE, IMG_W, IMG_H, 3])
y_ = tf.placeholder(tf.int32, shape=[BATCH_SIZE])
keep_prob = tf.placeholder(tf.float32)  #损失率

logits = model.inference(x, BATCH_SIZE, N_CLASSES, keep_prob)
loss = model.losses(logits, y_)  #计算误差
acc = model.evaluation(logits, y_)  #计算准确度
train_op = model.trainning(loss, learning_rate)  #训练开始程序

with tf.Session() as sess:
    saver = tf.train.Saver()  #运行队列准备等