Custom cost function is not being used for training
- Dominant language
- No language data
- Stars
- 369
- Forks
- 63
- PR merge metrics
- No merged PRs in 30d
Description
Hi.
I have implemented my own cost function but I have realized it has only being used for printing, not for calculating the error for the gradient descend, and in fact the metric that is being used during the training is the default one (the accuracy).
The reason I am saying this is because I have tried returning always value 1 (`return [(:EuclideanDist, 1)]`)in the mx.get method and I still get exactly the same results (when in this case it should not be able to even learn anything).
Thanks for your help.
```
using MXNet
using Distances
import MXNet.mx: get, reset!, update!
redirect_stderr(STDOUT)
srand(1234)
type EuclideanDist <: mx.AbstractEvalMetric
loss_sum :: Float64
n_sample :: Int
EuclideanDist() = new(0.0, 0)
end
function mx.update!(metric :: EuclideanDist, labels :: Vector{mx.NDArray}, preds :: Vector{mx.NDArray})
preds = copy(preds)
labels = copy(labels)
@assert length(labels) == length(preds)
loss = 0.0
for (label, pred) in zip(labels, preds)
@mx.nd_as_jl ro=(label, pred) begin
for elem in 1:size(label)[2]
_label = label[:, elem]
_pred = pred[:, elem]
_euc = euclidean([_label[1]/10000 * training_deg_to_m_lat, _label[2]/10000 * training_deg_to_m_long], [_pred[1]/10000 * training_deg_to_m_lat, _pred[2]/10000 * training_deg_to_m_long])
loss += _euc
end
end
end
metric.loss_sum += loss
metric.n_sample += size(labels[1])[2]
end
function mx.get(metric :: EuclideanDist)
distance = metric.loss_sum / metric.n_sample
return [(:EuclideanDist, distance)]
end
function mx.reset!(metric :: EuclideanDist)
metric.loss_sum = 0.0
metric.n_sample = 0
end
data = mx.Variable(:data) # Do not change the name
lbl = mx.Variable(:softmax_label) # Do not change the name
fc1 = mx.FullyConnected(data, name=:fc1, num_hidden=512)
act1 = mx.Activation(fc1, name=:relu1, act_type=:relu)
fc2 = mx.FullyConnected(act1, name=:fc2, num_hidden=512)
act2 = mx.Activation(fc2, name=:relu2, act_type=:relu)
fc3 = mx.FullyConnected(act2, name=:fc3, num_hidden=128)
act3 = mx.Activation(fc3, name=:relu3, act_type=:relu)
fc4 = mx.FullyConnected(act3, name=:fc4, num_hidden=32)
act4 = mx.Activation(fc4, name=:relu4, act_type=:relu)
fc5 = mx.FullyConnected(act4, name=:fc5, num_hidden=2)
mlp = mx.LinearRegressionOutput(fc5, lbl, name=:linear)
# data provider
train_provider = mx.ArrayDataProvider(Array(training_data)', Array(training_labels)', batch_size = 100, shuffle = true)
eval_provider = mx.ArrayDataProvider(Array(validation_data)', Array(validation_labels)', batch_size = 100, shuffle = true)
# setup model
model = mx.FeedForward(mlp, context=mx.gpu(1))
# optimizer
optimizer = mx.ADAM()
# Initializer
#initializer = mx.XavierInitializer(distribution = mx.xv_uniform, regularization = mx.xv_avg, magnitude = 3)
initializer = mx.UniformInitializer(0.01)
# fit parameters
a = mx.fit(model, optimizer, train_provider, eval_data=eval_provider, initializer=initializer, n_epoch=200, eval_metric=EuclideanDist())
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.