我正在尝试训练对数线性语言模型。为了做到这一点,我需要最大化矢量参数。 我正在使用这个Loss函数: Loss function
这是我的代码:
v0 = np.ones((len(tag_list), 1))
def first_argument(v, x, current_Tag):
return np.dot(v, unigram_tag_feature_vector(current_Tag))
def second_argument(v, x, taglist):
exp_ = 0
for tag in taglist:
exp_ += np.dot(v, unigram_tag_feature_vector(tag))
return np.log(exp_)
def sum_func(ordered_text, taglist, v):
result = 0
for (word, tag) in ordered_text:
result += (-first_argument(v, 0, tag)- second_argument(v, 0, taglist))
return result
def func(params, *args):
ordered_text = args[0]
taglist = args[1]
v = params
v_model= sum_func(ordered_text, taglist, v)
print v_model
return v
res = scipy.optimize.minimize(func, x0=v0, args=(ordered_text, tag_list))
我得到的错误是:
line 610, in approx_fprime
grad[k] = (f(*((xk + d,) + args)) - f0) / d[k]
ValueError: setting an array element with a sequence.
args如下:
ordered_text=[('In', 'IN'), ('an', 'DT'), ('Oct.', 'NNP'), ('19', 'CD'), ....]
tag_list=['CC', 'CD', 'DT', 'EX', 'FW'....]
函数unigram_tag_feature_vector
返回以下输出:
[ 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
(稀疏矢量,用于任何输入)
答案 0 :(得分:0)
错误消息意味着numpy正在获得一个它期望标量值的向量。您的目标函数返回参数向量v
。你的意思是它返回标量v_model
?