0

我想創建一個簡單的自動編碼3000的輸入,2隱藏和3000個輸出神經元:獲得尺寸不對創造Theano前饋的自動編碼時/千層麪

def build_autoencoder(input_var=None): 
    l_in = InputLayer(shape=(None,3000), input_var=input_var) 

    l_hid = DenseLayer(
      l_in, num_units=2, 
      nonlinearity=rectify, 
      W=lasagne.init.GlorotUniform()) 

    l_out = DenseLayer(
      l_hid, num_units=3000, 
      nonlinearity=softmax) 

    return l_out 

訓練數據的形狀如下:

train.shape = (3000,3) 

這是輸入,目標和損失函數定義:

import sys 
import os 
import time 
import numpy as np 
import theano 
import theano.tensor as T 
import lasagne 
from lasagne.updates import rmsprop 
from lasagne.layers import DenseLayer, DropoutLayer, InputLayer 
from lasagne.nonlinearities import rectify, softmax 
from lasagne.objectives import categorical_crossentropy 
# Creating the Theano variables 
input_var = T.dmatrix('inputs') 
target_var = T.dmatrix('targets') 

# Building the Theano expressions on these variables 
network = build_autoencoder(input_var) 

prediction = lasagne.layers.get_output(network) 
loss = categorical_crossentropy(prediction, target_var) 
loss = loss.mean() 

test_prediction = lasagne.layers.get_output(network, 
                deterministic=True) 
test_loss = categorical_crossentropy(test_prediction, target_var) 
test_loss = test_loss.mean() 
test_acc = T.mean(T.eq(T.argmax(test_prediction, axis=1), target_var), 
          dtype=theano.config.floatX) 

我只是運行一個時期,但得到一個錯誤:

params = lasagne.layers.get_all_params(network, trainable=True) 
updates = rmsprop(loss, params, learning_rate=0.001) 

# Compiling the graph by declaring the Theano functions 

train_fn = theano.function([input_var, target_var], 
            loss, updates=updates) 
val_fn = theano.function([input_var, target_var], 
           [test_loss, test_acc]) 

# For loop that goes each time through the hole training 
# and validation data 
print("Starting training...") 
for epoch in range(1): 

    # Going over the training data 
    train_err = 0 
    train_batches = 0 
    start_time = time.time() 
    print 'test1' 
    train_err += train_fn(train, train) 
    train_batches += 1 

    # Going over the validation data 
    val_err = 0 
    val_acc = 0 
    val_batches = 0 
    err, acc = val_fn(train, train) 
    val_err += err 
    val_acc += acc 
    val_batches += 1 

    # Then we print the results for this epoch: 
    print("Epoch {} of {} took {:.3f}s".format(epoch + 1, num_epochs, time.time() - start_time)) 
    print("training loss:\t\t{:.6f}".format(train_err/train_batches)) 
    print("validation loss:\t\t{:.6f}".format(val_err/val_batches)) 
    print("validation accuracy:\t\t{:.2f} %".format(val_acc/val_batches * 100)) 

這是錯誤:

ValueError: ('shapes (3000,3) and (3000,2) not aligned: 3 (dim 1) != 3000 (dim 0)', (3000, 3), (3000, 2)) Apply node that caused the error: Dot22(inputs, W) Toposort index: 3 Inputs types: [TensorType(float64, matrix), TensorType(float64, matrix)] Inputs shapes: [(3000, 3), (3000, 2)] Inputs strides: [(24, 8), (16, 8)] Inputs values: ['not shown', 'not shown'] Outputs clients: [[Elemwise{add,no_inplace}(Dot22.0, InplaceDimShuffle{x,0}.0), Elemwise{Composite{(i0 * (Abs(i1) + i2 + i3))}}[(0, 2)](TensorConstant{(1, 1) of 0.5}, Elemwise{add,no_inplace}.0, Dot22.0, InplaceDimShuffle{x,0}.0)]]

對我來說,似乎是自動編碼器的瓶頸問題。有任何想法嗎?

回答

0

剛剛從我的IBM大學(Erwan)一些幫助,我已經張貼工作解決這個GIST,相關的部分是這些的:

首先,得到正確的訓練數據的形狀:

train.shape = (3, 3000) 

然後使用相同形狀的InputLayer:

def build_autoencoder(input_var=None): 
    l_in = InputLayer(shape=(3, 3000), input_var=input_var) 

    l_hid = DenseLayer(
      l_in, num_units=2, 
      nonlinearity=rectify, 
      W=lasagne.init.GlorotUniform()) 

    l_out = DenseLayer(
      l_hid, num_units=3000, 
      nonlinearity=softmax) 

return l_out 

所以這個解決了,接下來的問題是得到一個降序在培訓期間的成本,但這是另一個topic :)