Отслеживайте тайники в нейронной сети - PullRequest
0 голосов
/ 31 декабря 2018

Я попытался реализовать модель нейронной сети L-слоя, основанную на курсе глубокого обучения Coursera, но у меня возникла проблема с добавлением кэша в список кэшей.

caches = list.append(cache)

В курсе предлагается использовать функцию list.append ().Остальные коды приведены ниже:

def L_model_forward(X, parameters):
    """
    Implement forward propagation 

    Arguments:
    X -- data, numpy array of shape (input size, number of examples)
    parameters -- output of initialize_parameters_deep()

    Returns:
    AL -- last post-activation value
    caches -- list of caches containing:
                every cache of linear_activation_forward() (there are L-1 of them, indexed from 0 to L-1)
    """

    caches = []
    A = X
    L = len(parameters) // 2   # number of layers in the neural network

    # Implement [LINEAR -> RELU]*(L-1). Add "cache" to the "caches" list.
    for l in range(1, L):
        A_prev = A 
        ### START CODE HERE ### (≈ 2 lines of code)
        A, cache = linear_activation_forward(A_prev, parameters['W' + str(l)], parameters['b' + str(l)], activation = "relu")
        caches = list.append(cache)
        ### END CODE HERE ###

    # Implement LINEAR -> SIGMOID. Add "cache" to the "caches" list.
    ### START CODE HERE ### (≈ 2 lines of code)
    AL, cache =  linear_activation_forward(A, parameters['W' + str(L)], parameters['b' + str(L)], activation = "sigmoid")
    caches = list.append(cache)    
    ### END CODE HERE ###

    assert(AL.shape == (1,X.shape[1]))

    return AL, caches

Когда я запускаю коды, вот ошибка :


TypeError: descriptor 'append 'требует объекта' list ', но получил' tuple '

1 Ответ

0 голосов
/ 31 декабря 2018

Вы не можете добавить в список, используя caches = list.append(cache).К нему необходимо добавить caches.append(cache), а также перед добавлением в caches преобразовать кэш кортежей в список с помощью cache=list(cache), а затем добавить в список кешей с помощью caches.append(cache).

Ваш код будет выглядеть следующим образом:-

def L_model_forward(X, parameters):

    caches = []
    A = X
    L = len(parameters) // 2   # number of layers in the neural network
    for l in range(1, L):
        A_prev = A 

        A, cache = linear_activation_forward(A_prev, parameters['W' + str(l)], parameters['b' + str(l)], activation = "relu")
        caches.append(list(cache))


    AL, cache =  linear_activation_forward(A, parameters['W' + str(L)], parameters['b' + str(L)], activation = "sigmoid")
    caches.append(list(cache))    

    assert(AL.shape == (1,X.shape[1]))

    return AL, caches
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...