def sentence_to_avg(sentence, word_to_vec_map):
"""
Converts a sentence (string) into a list of words (strings). Extracts the GloVe representation of each word
and averages its value into a single vector encoding the meaning of the sentence.
Arguments:
sentence -- string, one training example from X
word_to_vec_map -- dictionary mapping every word in a vocabulary into its 50-dimensional vector representation
Returns:
avg -- average vector encoding information about the sentence, numpy-array of shape (J,), where J can be any number
"""
# Get a valid word contained in the word_to_vec_map.
any_word = list(word_to_vec_map.keys())[0]
### START CODE HERE ###
# Step 1: Split sentence into list of lower case words (≈ 1 line)
words = sentence.lower().split()
# Initialize the average word vector, should have the same shape as your word vectors.
avg = np.zeros(word_to_vec_map[any_word].shape)
# Initialize count to 0
count = 0
# Step 2: average the word vectors. You can loop over the words in the list "words".
for w in words:
# Check that word exists in word_to_vec_map
if w in word_to_vec_map:
avg += word_to_vec_map[w]
# Increment count
count +=1
if count > 0:
# Get the average. But only if count > 0
avg = avg / count # 여기서 np.mean을 사용하지 말 것.
### END CODE HERE ###
return avg
핵심이 되는 에러 부분이 avg = avg / count인데, 이 코드는 정답이고 avg = np.mean(avg)을 사용해서 코드를 짜면 avg 차원 전체에 대한 평균값이 구해져서 Scalar로 값이 반환된다.
여기서 원하는 것은 벡터 형태로 값이 반환되어야하기 때문에 스칼라 연산 결과 값이 아닌 벡터 연산으로 진행하게끔 하기 위해 avg / count를 쓴다.
에러 설명
avg 변수와
word_to_vec_map.shape
가 차원이 안맞아서 에러가 발생했다.에러 결과 :
"Check the shape of your avg array"
하단 코드 참고
핵심이 되는 에러 부분이
avg = avg / count
인데, 이 코드는 정답이고avg = np.mean(avg)
을 사용해서 코드를 짜면 avg 차원 전체에 대한 평균값이 구해져서 Scalar로 값이 반환된다.여기서 원하는 것은 벡터 형태로 값이 반환되어야하기 때문에 스칼라 연산 결과 값이 아닌 벡터 연산으로 진행하게끔 하기 위해
avg / count
를 쓴다.별 것 아닌데 딥러닝하다보면 계속 차원 관련된 이슈가 걸려서 헷갈린다.