Im trying to penalize some predictions.
Building this cost function didnt work.


*Reference :* 
http://stackoverflow.com/questions/39412051/how-to-penalize-predictions-binary-cross-entropy-and-conv-nets






prediction = lasagne.layers.get_output(model)
#
def w_categorical_crossentropy(y_true, y_pred, weights):
    from itertools import product
    import theano.tensor as T
    nb_cl = len(weights)
    final_mask = T.zeros_like(y_pred[:, 0])
    y_pred_max = T.max(y_pred, axis=1)
    y_pred_max = T.reshape(y_pred_max, (T.shape(y_pred)[0], 1))

    y_pred_max_mat = T.eq(y_pred, y_pred_max)
    
    for c_p, c_t in product(range(nb_cl), range(nb_cl)):
        final_mask += (weights[c_t, c_p] * y_pred_max_mat[:, c_p] * y_true[:, 
c_t])

    return lasagne.objectives.binary_crossentropy(y_pred, y_true) * final_mask


#
cost_weights = np.array([[0, 0.25], [0.5, 0]])
cost_weights = cost_weights.astype(np.float32)


loss_or_grads = 
w_categorical_crossentropy(y_pred=prediction,y_true=self.target_var,weights=cost_weights)
loss_or_grads = loss_or_grads.mean()

...






----

Generating this error :


Traceback (most recent call last):
  File "Main.py", line 292, in <module>
    main()
  File "Main.py", line 213, in main
    max_patience=max_patience
  File 
"/home/andrelopes/private/ProjetoMestrado/TriosConvNet/ConvolutionalNeuralNetwork.py",
 line 226, in train
    train_err += training_function(inputs, targets)
  File 
"/home/andrelopes/private/ProjetoMestrado/virtualenv-15.0.3/myVE/local/lib/python2.7/site-packages/Theano-0.8.1-py2.7.egg/theano/compile/function_module.py",
 line 871, in __call__
    storage_map=getattr(self.fn, 'storage_map', None))
  File 
"/home/andrelopes/private/ProjetoMestrado/virtualenv-15.0.3/myVE/local/lib/python2.7/site-packages/Theano-0.8.1-py2.7.egg/theano/gof/link.py",
 line 314, in raise_with_op
    reraise(exc_type, exc_value, exc_trace)
  File 
"/home/andrelopes/private/ProjetoMestrado/virtualenv-15.0.3/myVE/local/lib/python2.7/site-packages/Theano-0.8.1-py2.7.egg/theano/compile/function_module.py",
 line 859, in __call__
    outputs = self.fn()
IndexError: index out of bounds
Apply node that caused the error: GpuSubtensor{::, int64}(GpuFromHost.0, 
Constant{1})
Toposort index: 73
Inputs types: [CudaNdarrayType(float32, matrix), Scalar(int64)]
Inputs shapes: [(3000, 1), ()]
Inputs strides: [(1, 0), ()]
Inputs values: ['not shown', 1]
Outputs clients: [[GpuDimShuffle{x,0}(GpuSubtensor{::, int64}.0)]]

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"theano-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to