栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在DNN训练结束时返回Hessian逆矩阵,并使用输入进行偏导

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在DNN训练结束时返回Hessian逆矩阵,并使用输入进行偏导

答案:该代码用于预测模型的一个输出值。目前,我正在对其进行修改,以计算3个jacobian矩阵;每个输出一个。

import theanoimport theano.tensor as Timport theano.typed_listtheano.config.optimizer='fast_compile'theano.config.exception_verbosity='high'# Declare function input placeholdersweights_in_model = theano.typed_list.TypedListType(theano.tensor.dmatrix)()x = T.matrix('x')# Define model functiondef pred(x,weights_in_model):     weights = T.concatenate((weights_in_model[0],weights_in_model[1]), axis=0)    x = T.concatenate((x, T.ones((T.shape(x)[0], 1))), axis=1)    prediction = T.dot(x, weights)    prediction = T.clip(prediction, 0, 9999.)    weights = T.concatenate((weights_in_model[2],weights_in_model[3]), axis=0)    prediction = T.concatenate((prediction, T.ones((T.shape(prediction)[0], 1))), axis=1)    prediction = T.dot(prediction, weights)    prediction = T.clip(prediction, 0, 9999.)    weights = T.concatenate((weights_in_model[4],weights_in_model[5]), axis=0)    prediction = T.concatenate((prediction, T.ones((T.shape(prediction)[0], 1))), axis=1)    prediction = T.dot(prediction, weights)    prediction = T.clip(prediction, 0, 9999.)    weights = T.concatenate((weights_in_model[6],weights_in_model[7]), axis=0)    prediction = T.concatenate((prediction, T.ones((T.shape(prediction)[0], 1))), axis=1)    prediction = T.dot(prediction, weights)    prediction = T.clip(prediction, 0, 9999.)    weights = T.concatenate((weights_in_model[8],weights_in_model[9]), axis=0)    prediction = T.concatenate((prediction, T.ones((T.shape(prediction)[0], 1))), axis=1)    prediction = T.dot(prediction, weights)    prediction = T.clip(prediction, 0, 9999.)    weights = T.concatenate((weights_in_model[10],weights_in_model[11]), axis=0)    prediction = T.concatenate((prediction, T.ones((T.shape(prediction)[0], 1))), axis=1)    prediction = T.dot(prediction, weights)    prediction = T.clip(prediction, 0, 9999.)    weights = T.concatenate((weights_in_model[12],weights_in_model[13]), axis=0)    prediction = T.concatenate((prediction, T.ones((T.shape(prediction)[0], 1))), axis=1)    prediction = T.dot(prediction, weights)    prediction = T.flatten(prediction)    return prediction# Create gradient functionf=theano.gradient.jacobian(pred(x,weights_in_model),wrt=x)# Compile functionh=theano.function([x,weights_in_model],f,allow_input_downcast=True)# Get function inputsweights_in_model_ = model.get_weights()x_=train_data# Reshape bias layersweights_in_model_[1] = np.reshape(weights_in_model_[1], (1, 2048))weights_in_model_[3] = np.reshape(weights_in_model_[3], (1, 1024))weights_in_model_[5] = np.reshape(weights_in_model_[5], (1, 512))weights_in_model_[7] = np.reshape(weights_in_model_[7], (1, 256))weights_in_model_[9] = np.reshape(weights_in_model_[9], (1, 128))weights_in_model_[11] = np.reshape(weights_in_model_[11], (1, 32))weights_in_model_[13] = np.reshape(weights_in_model_[13], (1, 1))# Compute Jacobian (returns format with a bunch of zero rows)jacs = h(x_, weights_in_model_)# Put Jacobian matrix in proper format (ie. shape = (number_of_input_examples, number_of_input_features)jacobian_matrix = np.zeros((jacs.shape[0],jacs.shape[2]))for i, jac in enumerate(jacs):     jacobian_matrix[i] = jac[i]

下一个任务是找到模型权重的输出的黑森州矩阵!



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/659755.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号