2015-06-25 45 views
1

我試圖訓練神經網絡來學習函數y = x1 + x2 + x3。目標是與Caffe一起玩,以便更好地學習和理解它。所需的數據是在python中合成生成的,並作爲lmdb數據庫文件寫入內存。Caffe:在學習簡單的線性函數時損失極高

數據生成代碼:

import numpy as np 
import lmdb 
import caffe 

Ntrain = 100 
Ntest = 20 
K = 3 
H = 1 
W = 1 

Xtrain = np.random.randint(0,1000, size = (Ntrain,K,H,W)) 
Xtest = np.random.randint(0,1000, size = (Ntest,K,H,W)) 

ytrain = Xtrain[:,0,0,0] + Xtrain[:,1,0,0] + Xtrain[:,2,0,0] 
ytest = Xtest[:,0,0,0] + Xtest[:,1,0,0] + Xtest[:,2,0,0] 

env = lmdb.open('expt/expt_train') 

for i in range(Ntrain): 
    datum = caffe.proto.caffe_pb2.Datum() 
    datum.channels = Xtrain.shape[1] 
    datum.height = Xtrain.shape[2] 
    datum.width = Xtrain.shape[3] 
    datum.data = Xtrain[i].tobytes() 
    datum.label = int(ytrain[i]) 
    str_id = '{:08}'.format(i) 

    with env.begin(write=True) as txn: 
     txn.put(str_id.encode('ascii'), datum.SerializeToString()) 


env = lmdb.open('expt/expt_test') 

for i in range(Ntest): 
    datum = caffe.proto.caffe_pb2.Datum() 
    datum.channels = Xtest.shape[1] 
    datum.height = Xtest.shape[2] 
    datum.width = Xtest.shape[3] 
    datum.data = Xtest[i].tobytes() 
    datum.label = int(ytest[i]) 
    str_id = '{:08}'.format(i) 

    with env.begin(write=True) as txn: 
     txn.put(str_id.encode('ascii'), datum.SerializeToString()) 

Solver.prototext文件:

net: "expt/expt.prototxt" 

display: 1 
max_iter: 200 
test_iter: 20 
test_interval: 100 

base_lr: 0.000001 
momentum: 0.9 
# weight_decay: 0.0005 

lr_policy: "inv" 
# gamma: 0.5 
# stepsize: 10 
# power: 0.75 

snapshot_prefix: "expt/expt" 
snapshot_diff: true 

solver_mode: CPU 
solver_type: SGD 

debug_info: true 

來自Caffe型號:

name: "expt" 


layer { 
    name: "Expt_Data_Train" 
    type: "Data" 
    top: "data" 
    top: "label"  

    include { 
     phase: TRAIN 
    } 

    data_param { 
     source: "expt/expt_train" 
     backend: LMDB 
     batch_size: 1 
    } 
} 


layer { 
    name: "Expt_Data_Validate" 
    type: "Data" 
    top: "data" 
    top: "label"  

    include { 
     phase: TEST 
    } 

    data_param { 
     source: "expt/expt_test" 
     backend: LMDB 
     batch_size: 1 
    } 
} 


layer { 
    name: "IP" 
    type: "InnerProduct" 
    bottom: "data" 
    top: "ip" 

    inner_product_param { 
     num_output: 1 

     weight_filler { 
      type: 'constant' 
     } 

     bias_filler { 
      type: 'constant' 
     } 
    } 
} 


layer { 
    name: "Loss" 
    type: "EuclideanLoss" 
    bottom: "ip" 
    bottom: "label" 
    top: "loss" 
} 

上我得到了測試數據的損失233,655。這是令人震驚的,因爲損失比訓練和測試數據集中的數字大三個數量級。另外,要學習的功能是簡單的線性函數。我似乎無法弄清楚代碼中的錯誤。任何建議/投入都非常感謝。

回答

1

損失產生很多在這種情況下,因爲來自Caffe僅在int32格式uint8格式和標籤(datum.label)接受數據(即datum.data)。但是,對於標籤,numpy.int64格式也似乎工作。我認爲datum.data僅適用於uint8格式,因爲Caffe主要是爲計算機視覺任務開發的,其輸入是圖像,其RGB值在[0,255]範圍內。 uint8可以使用最少量的內存來捕獲此信息。我做了如下修改數據生成代碼:

Xtrain = np.uint8(np.random.randint(0,256, size = (Ntrain,K,H,W))) 
Xtest = np.uint8(np.random.randint(0,256, size = (Ntest,K,H,W))) 

ytrain = int(Xtrain[:,0,0,0]) + int(Xtrain[:,1,0,0]) + int(Xtrain[:,2,0,0]) 
ytest = int(Xtest[:,0,0,0]) + int(Xtest[:,1,0,0]) + int(Xtest[:,2,0,0]) 

與網絡參數玩耍後(學習率,迭代次數等),我得到的10 ^(數量級的錯誤 - 6 )我認爲這很不錯!