Giter Club home page Giter Club logo

Comments (13)

stared avatar stared commented on August 29, 2024

With Keras 2.0.5 it works well.

In any case - how did you specify the loss function? (Could you provide code, at least model.compile and model.fit lines?)

from livelossplot.

fculinovic avatar fculinovic commented on August 29, 2024

Model is compiled with

model.compile(loss=keras.losses.binary_crossentropy,
              optimizer=keras.optimizers.Adam(lr=1e-3, amsgrad=True),
             metrics=['accuracy'])

Fitting is done according to your example:

model.fit(data[m_train], labels[m_train],
    epochs=10, verbose=True,
    validation_data=(data[m_val], labels[m_val]),
    callbacks=[checkpointer, PlotLossesKeras()]
)

where your example does

from livelossplot import PlotLossesKeras

model.fit(X_train, Y_train,
          epochs=10,
          validation_data=(X_test, Y_test),
          callbacks=[PlotLossesKeras()],
          verbose=0)

Printing out model.loss shows that the return value of that call is the function itself
<function binary_crossentropy at 0x7fa918886598>

The stacktrace is:

TypeError                                 Traceback (most recent call last)
<ipython-input-99-baaad69844e3> in <module>()
      2     epochs=10, verbose=True,
      3     validation_data=(data[m_val], labels[m_val]),
----> 4     callbacks=[PlotLossesKeras()]
      5 )

~/.local/lib/python3.5/site-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
   1667                               initial_epoch=initial_epoch,
   1668                               steps_per_epoch=steps_per_epoch,
-> 1669                               validation_steps=validation_steps)
   1670 
   1671     def evaluate(self, x=None, y=None,

~/.local/lib/python3.5/site-packages/keras/engine/training.py in _fit_loop(self, f, ins, out_labels, batch_size, epochs, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics, initial_epoch, steps_per_epoch, validation_steps)
   1136             'metrics': callback_metrics or [],
   1137         })
-> 1138         callbacks.on_train_begin()
   1139         callback_model.stop_training = False
   1140         for cbk in callbacks:

~/.local/lib/python3.5/site-packages/keras/callbacks.py in on_train_begin(self, logs)
    127         logs = logs or {}
    128         for callback in self.callbacks:
--> 129             callback.on_train_begin(logs)
    130 
    131     def on_train_end(self, logs=None):

~/.local/lib/python3.5/site-packages/livelossplot/keras_plot.py in on_train_begin(self, logs)
     31                 ((len(self.base_metrics) + 1) // self.max_cols + 1) * self.cell_size[1]
     32             )
---> 33         self.metric2printable['loss'] = self.metric2printable.get(self.model.loss, self.model.loss) + " (cost function)"
     34         self.max_epoch = self.params['epochs'] if not self.dynamic_x_axis else None
     35 

TypeError: unsupported operand type(s) for +: 'function' and 'str'

from livelossplot.

utausheva avatar utausheva commented on August 29, 2024

Got the same error

from livelossplot.

stared avatar stared commented on August 29, 2024

Solved with this commit: a9734f2

Let me know if it works.

from livelossplot.

utausheva avatar utausheva commented on August 29, 2024

Nothing changed((

from livelossplot.

fculinovic avatar fculinovic commented on August 29, 2024

This fixed the issue at my end. You sure you have used pip3 install git+git://github.com/stared/livelossplot.git@a9734f2488d10e568fbf12bc35e39396d782db9e --user ?

Here's the ending of this command

Installing collected packages: livelossplot
  Found existing installation: livelossplot 0.1.1
    Uninstalling livelossplot-0.1.1:
      Successfully uninstalled livelossplot-0.1.1
  Running setup.py install for livelossplot ... done
Successfully installed livelossplot-0.1.2

from livelossplot.

utausheva avatar utausheva commented on August 29, 2024

I do. Error changed: now its:
Traceback (most recent call last):
File "C:/Users/ASUS/PycharmProjects/achimovka/train.py", line 108, in
shuffle=True)
File "C:\Users\ASUS\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\models.py", line 963, in fit
validation_steps=validation_steps)
File "C:\Users\ASUS\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\training.py", line 1705, in fit
validation_steps=validation_steps)
File "C:\Users\ASUS\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\training.py", line 1163, in _fit_loop
callbacks.on_train_begin()
File "C:\Users\ASUS\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\callbacks.py", line 130, in on_train_begin
callback.on_train_begin(logs)
File "C:\Users\ASUS\AppData\Local\Programs\Python\Python36\lib\site-packages\livelossplot\keras_plot.py", line 42, in on_train_begin
self.metric2printable['loss'] = self.metric2printable.get(loss_name, loss_name) + " (cost function)"
TypeError: unhashable type: 'list'

from livelossplot.

fculinovic avatar fculinovic commented on August 29, 2024

Could you provide the parts of code regarding the compile and fit functions?

from livelossplot.

utausheva avatar utausheva commented on August 29, 2024

model.compile(optimizer=opt, loss=['mae'], metrics=['mae'])
plot_losses = livelossplot.PlotLossesKeras()

history = model.fit(X_train, Y_train,
nb_epoch=10,
batch_size=2,
verbose=0,
validation_data=(X_test, Y_test),
callbacks=[reduce_lr,checkpointer,plot_losses],
shuffle=True)

from livelossplot.

fculinovic avatar fculinovic commented on August 29, 2024

Ok, so it seems that this is the case:

if the loss is defined as
model.compile(..., loss= 'mae', ...)
or
model.compile(..., loss=keras.losses.mean_absolute_error)
it should work.

This is a solution if you have only 1 loss function. As for more loss functions, it seems that
self.metric2printable['loss'] = self.metric2printable.get(loss_name, loss_name) + " (cost function)"

and the accompanying function

def loss2name(loss):
    if hasattr(loss, '__call__'):
        # if passed as a function
        return loss.__name__
    else:
        # if passed as a string
        return loss

aren't written to accomodate multiple losses

from livelossplot.

stared avatar stared commented on August 29, 2024

@utausheva Write
model.compile(optimizer=opt, loss='mae', metrics=['mae']) and it will be all right (and actually in your case metrics is not needed as we keep tract of 'mae' anyway).

For typical networks there is only one loss function, see https://keras.io/models/model/#compile.
I need to dive into model.compile to make sure I pre-process data in the same way.

from livelossplot.

stared avatar stared commented on August 29, 2024

@utausheva Please open a separate issue with PyCharm. Make sure to provide complete examples and versions. (In this thread I am deleting off-topic comments.)

from livelossplot.

stared avatar stared commented on August 29, 2024

Looked at https://github.com/keras-team/keras/blob/master/keras/engine/training.py.

Now loss can be a list or a dict, see: 8abe5f4.

from livelossplot.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.