RyanKor / 2021-google-ml-bootcamp

Google Machine Learning Bootcamp 2021
13 stars 0 forks source link

[Assignment Error] Course 5, Week 1: AttributeError in music_inference_model(LSTM_cell, densor, Ty=100) #17

Closed RyanKor closed 3 years ago

RyanKor commented 3 years ago

Error Explanation

AttributeError: The layer “lstm” has multiple inbound nodes, with different output shapes. Hence the notion of “output shape” is ill-defined for the layer. Use get_output_shape_at(node_index) instead.

# UNQ_C2 (UNIQUE CELL IDENTIFIER, DO NOT EDIT)
# GRADED FUNCTION: music_inference_model

def music_inference_model(LSTM_cell, densor, Ty=100):
    """
    Uses the trained "LSTM_cell" and "densor" from model() to generate a sequence of values.

    Arguments:
    LSTM_cell -- the trained "LSTM_cell" from model(), Keras layer object
    densor -- the trained "densor" from model(), Keras layer object
    Ty -- integer, number of time steps to generate

    Returns:
    inference_model -- Keras model instance
    """

    # Get the shape of input values
    n_values = densor.units
    # Get the number of the hidden state vector
    n_a = LSTM_cell.units

    # Define the input of your model with a shape 
    x0 = Input(shape=(1, n_values))

    # Define s0, initial hidden state for the decoder LSTM
    a0 = Input(shape=(n_a,), name='a0')
    c0 = Input(shape=(n_a,), name='c0')
    a = a0
    c = c0
    x = x0

    ### START CODE HERE ###
    # Step 1: Create an empty list of "outputs" to later store your predicted values (≈1 line)
    outputs = []

    # Step 2: Loop over Ty and generate a value at every time step
    for t in range(Ty):
        # Step 2.A: Perform one step of LSTM_cell. Use "x", not "x0" (≈1 line)
        a, _, c = LSTM_cell(inputs=x, initial_state=[a,c])

        # Step 2.B: Apply Dense layer to the hidden state output of the LSTM_cell (≈1 line)
        out = densor(a)
        # Step 2.C: Append the prediction "out" to "outputs". out.shape = (None, 90) (≈1 line)
        outputs.append(out)

        # Step 2.D: 
        # Select the next value according to "out",
        # Set "x" to be the one-hot representation of the selected value
        # See instructions above.
        x = tf.math.argmax(out, axis= -1) # 여기서 차원 변환 에러 발생
        x = tf.one_hot(x, depth=n_values)
        # Step 2.E: 
        # Use RepeatVector(1) to convert x into a tensor with shape=(None, 1, 90)
        x = RepeatVector(1)(x)

    # Step 3: Create model instance with the correct "inputs" and "outputs" (≈1 line)
    inference_model = Model(inputs=[x0, a0, c0], outputs=outputs)

    ### END CODE HERE ###

    return inference_model

Error Solution

        x = tf.math.argmax(out, axis= -1) # 여기서 차원 변환 에러 발생
        x = tf.one_hot(x, depth=n_values)

이 2개의 코드에서 차원 변환하는 게 상당히 애먹어서 deeplearning.ai에서 수강생들이 남긴 에러를 보고 해결할 수 있게 되었다.

주어진 Tensor를 (None, 90)으로 변경하고 (axis = -1) 그 후, one hot encoding 기법으로 (None, 90,90)으로 변경한다.

최종적으로 RepeatVector(1)(x)를 사용해서 (None, 1, 90) 텐서로 변경하는데 계속 결과 값이 (90, 1, 90)이 나와서 어떤 에러인지 해멨다.

구글에 올라온 다른 사람들 주피터 노트북 답을 보고 해결하려고 했는데, 정답이 아닌 경우가 대다수였고 (특히 axis 값이 최신화되면서 답이 달랐다)

주피터 노트북 커널이 반응이 느려서 그런지 고친 값이 빨리 반영 안되서 해멘 것도 한 몫했다.

이걸로만 한 시간 잡아먹은 듯 싶다.