答案:该代码用于预测模型的一个输出值。目前,我正在对其进行修改,以计算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]下一个任务是找到模型权重的输出的黑森州矩阵!



