首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自动编码器输出没有正确的形状

自动编码器输出没有正确的形状
EN

Stack Overflow用户
提问于 2018-05-13 00:51:29
回答 1查看 36关注 0票数 0

我创建了一个连续向autoencoder添加图层的类,目的是从数据中学习越来越高的特征表示。然而,在训练我的算法并使用预测函数后,输出形状不是正确的形状。

代码语言:javascript
复制
x: shape = (6000, 23)

alg = SuccessiveAutoencoder()

alg.fit(x, epochs = 10)

x_compressed = alg.predict(x)

x_compressed should have a shape (6000,5) but instead has (6000, 23)

有人能帮我一下吗?问题出在哪里?

请找到下面的代码:

代码语言:javascript
复制
class SuccessiveAutoencoder():
"""The algorithm stacks autoencoders one after others, with the idea to
learn from the data higher level features.

Attributes
-----------------   
- hidden_layers: number of hidden layers
- units_nbr: number of neurons in each hidden layer
- activation: type of activation to be used. Note that the activation of 
the decoder is hardcoded within the class, and is a sigmoid
- fine_tuning: if the algorithm train trains the full network once this one
has been built. It can be 'y' or 'n'
"""

def __init__(self, hidden_layers = 5, units_nbr = 5, activation = 'relu',
             fine_tuning = 'y'):
    """Initialize the algorithm"""

    self.hidden_layers = hidden_layers
    self.units_nbr = units_nbr
    self.activation = activation
    self.fine_tuning = fine_tuning

def create_model(self, X):
    """Create the inital autoencoder structure"""

    # Build simple autoencoder
    input_time_series = Input(shape = (X.shape[1], ), name = 'input')
    layer1 = Dense(units = self.units_nbr, activation = self.activation, 
                   name = 'hidden1')(input_time_series)
    decoded = Dense(units = X.shape[1], activation = 'sigmoid', name = 'output')(layer1)

    return Model(input_time_series, decoded)

def add_layer(self, model, incr):
    """Add a new layer, and make previous layers not trainable"""

    # Set previous layers not trainable
    for layer in model.layers[:-1]:
        layer.trainable = False

    # Output of previous layer
    out = model.layers[-2].output

    # Add the new layer
    layer_new = Dense(units = self.units_nbr, activation = self.activation,
                      name = 'hidden' + str(incr))(out)

    decoded = model.layers[-1](layer_new)

    return Model(model.layers[0].input, decoded)

def fit(self, X, epochs):
    """Run the algorithm, creating the autoencoder and calibrating it"""

    # Create the model and train it
    print('/ Training Hidden Layer 1')
    model = self.create_model(X)
    model.compile(loss = 'mean_squared_error', optimizer = 'adam')

    h = model.fit(X, X, epochs = epochs, verbose = 0)
    print('Last loss: {}'.format(h.history['loss'][-1]))

    # Incrementally add layer, and train these new layers
    for incr in range(2, self.hidden_layers + 1):
        print('/ Training Hidden Layer {}'.format(str(incr)))
        model = self.add_layer(model, incr)
        model.compile(loss = 'mean_squared_error', optimizer = 'adam')

        h = model.fit(X, X, epochs = epochs, verbose = 0)
        print('Last loss: {}'.format(h.history['loss'][-1]))

    # If the user wants to run the calibration again over the complete model
    if self.fine_tuning == 'y':    

        # Final training
        print('/ Final Tuning')
        for layer in model.layers:
            layer.trainable = True

        model.compile(loss = 'mean_squared_error', optimizer = 'adam')

        h = model.fit(X, X, epochs = epochs, verbose = 0)
        print('Last loss: {}'.format(h.history['loss'][-1]))

    # Get rid of last layer, and stored the model
    model.layers.pop()

    model.compile(loss = 'mean_squared_error', optimizer = 'adam')

    self.model = model

def predict(self, X):
    """Predict the compressed information from X"""

    return self.model.predict(X)
EN

回答 1

Stack Overflow用户

发布于 2018-05-13 00:56:47

代码的第103行,而不是使用

代码语言:javascript
复制
self.model = model

我把

代码语言:javascript
复制
self.model = Model(model.layers[0].input, model.layers[-1].output)

看起来它起作用了。但任何见解都是受欢迎的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50308604

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档