Skip to main content
replaced http://datascience.stackexchange.com/ with https://datascience.stackexchange.com/
Source Link

how would I have to format my data to make it work with Keras?

Your training labels in the output layer should be a binary vector that is 1 for class which is present and 0 for class which is not. For example, let us assume you have 3 classes of genres - comedy, romantic and horror. There are many ways to make it and Scikit-learn has a method which makes it very easy which I show below.

>>> from sklearn.preprocessing import MultiLabelBinarizer
>>> mlb = MultiLabelBinarizer()
>>> y = mlb.fit_transform([[0,2],[1]])
array([[1, 0, 1],
   [0, 1, 0]])

I initially considered using a softmax layer as my output layer, but since a movie can have multiple genre labels, how should my output be?

This is a simple Keras example I suggest.

>>> from keras.models import Sequential
>>> from keras.layers import Dense, Activation

>>> model = Sequential([
    Dense(32, input_dim=784),
    Activation('relu'),
    Dense(10),
    Activation('sigmoid'),
    ])
>>> model.compile(optimizer='rmsprop', loss='binary_crossentropy')
>>> model.fit(X_train, y_train)

Refer thisthis for more info. I used sigmoid because it is better for multilabel classification.

how would I have to format my data to make it work with Keras?

Your training labels in the output layer should be a binary vector that is 1 for class which is present and 0 for class which is not. For example, let us assume you have 3 classes of genres - comedy, romantic and horror. There are many ways to make it and Scikit-learn has a method which makes it very easy which I show below.

>>> from sklearn.preprocessing import MultiLabelBinarizer
>>> mlb = MultiLabelBinarizer()
>>> y = mlb.fit_transform([[0,2],[1]])
array([[1, 0, 1],
   [0, 1, 0]])

I initially considered using a softmax layer as my output layer, but since a movie can have multiple genre labels, how should my output be?

This is a simple Keras example I suggest.

>>> from keras.models import Sequential
>>> from keras.layers import Dense, Activation

>>> model = Sequential([
    Dense(32, input_dim=784),
    Activation('relu'),
    Dense(10),
    Activation('sigmoid'),
    ])
>>> model.compile(optimizer='rmsprop', loss='binary_crossentropy')
>>> model.fit(X_train, y_train)

Refer this for more info. I used sigmoid because it is better for multilabel classification.

how would I have to format my data to make it work with Keras?

Your training labels in the output layer should be a binary vector that is 1 for class which is present and 0 for class which is not. For example, let us assume you have 3 classes of genres - comedy, romantic and horror. There are many ways to make it and Scikit-learn has a method which makes it very easy which I show below.

>>> from sklearn.preprocessing import MultiLabelBinarizer
>>> mlb = MultiLabelBinarizer()
>>> y = mlb.fit_transform([[0,2],[1]])
array([[1, 0, 1],
   [0, 1, 0]])

I initially considered using a softmax layer as my output layer, but since a movie can have multiple genre labels, how should my output be?

This is a simple Keras example I suggest.

>>> from keras.models import Sequential
>>> from keras.layers import Dense, Activation

>>> model = Sequential([
    Dense(32, input_dim=784),
    Activation('relu'),
    Dense(10),
    Activation('sigmoid'),
    ])
>>> model.compile(optimizer='rmsprop', loss='binary_crossentropy')
>>> model.fit(X_train, y_train)

Refer this for more info. I used sigmoid because it is better for multilabel classification.

added 57 characters in body
Source Link
Hima Varsha
  • 2.4k
  • 15
  • 34

how would I have to format my data to make it work with Keras?

Your training labels in the output layer should be a one hot encodedbinary vector that is 1 for class which is present and 0 for class which is not. For example, let us assume you have 3 classes of genres - comedy, romantic and horror. There are many ways to make it and Scikit-learn has a method which makes it very easy which I show below.

>>> from sklearn.preprocessing import MultiLabelBinarizer
>>> mlb = MultiLabelBinarizer()
>>> y = mlb.fit_transform([[0,2],[1]])
array([[1, 0, 1],
   [0, 1, 0]])

I initially considered using a softmax layer as my output layer, but since a movie can have multiple genre labels, how should my output be?

This is a simple Keras example I suggest.

>>> from keras.models import Sequential
>>> from keras.layers import Dense, Activation

>>> model = Sequential([
    Dense(32, input_dim=784),
    Activation('relu'),
    Dense(10),
    Activation('sigmoid'),
    ])
>>> model.compile(optimizer='rmsprop', loss='categorical_crossentropy'loss='binary_crossentropy')
>>> model.fit(X_train, y_train)

Refer this for more info. I used sigmoid because it is better for multilabel classification.

how would I have to format my data to make it work with Keras?

Your training labels in the output layer should be a one hot encoded vector. For example, let us assume you have 3 classes of genres - comedy, romantic and horror. There are many ways to make it and Scikit-learn has a method which makes it very easy which I show below.

>>> from sklearn.preprocessing import MultiLabelBinarizer
>>> mlb = MultiLabelBinarizer()
>>> y = mlb.fit_transform([[0,2],[1]])
array([[1, 0, 1],
   [0, 1, 0]])

I initially considered using a softmax layer as my output layer, but since a movie can have multiple genre labels, how should my output be?

This is a simple Keras example I suggest.

>>> from keras.models import Sequential
>>> from keras.layers import Dense, Activation

>>> model = Sequential([
    Dense(32, input_dim=784),
    Activation('relu'),
    Dense(10),
    Activation('sigmoid'),
    ])
>>> model.compile(optimizer='rmsprop', loss='categorical_crossentropy')
>>> model.fit(X_train, y_train)

Refer this for more info. I used sigmoid because it is better for multilabel classification.

how would I have to format my data to make it work with Keras?

Your training labels in the output layer should be a binary vector that is 1 for class which is present and 0 for class which is not. For example, let us assume you have 3 classes of genres - comedy, romantic and horror. There are many ways to make it and Scikit-learn has a method which makes it very easy which I show below.

>>> from sklearn.preprocessing import MultiLabelBinarizer
>>> mlb = MultiLabelBinarizer()
>>> y = mlb.fit_transform([[0,2],[1]])
array([[1, 0, 1],
   [0, 1, 0]])

I initially considered using a softmax layer as my output layer, but since a movie can have multiple genre labels, how should my output be?

This is a simple Keras example I suggest.

>>> from keras.models import Sequential
>>> from keras.layers import Dense, Activation

>>> model = Sequential([
    Dense(32, input_dim=784),
    Activation('relu'),
    Dense(10),
    Activation('sigmoid'),
    ])
>>> model.compile(optimizer='rmsprop', loss='binary_crossentropy')
>>> model.fit(X_train, y_train)

Refer this for more info. I used sigmoid because it is better for multilabel classification.

added an example of keras
Source Link
Hima Varsha
  • 2.4k
  • 15
  • 34

how would I have to format my data to make it work with Keras?

Your training labels in the output layer should be a one hot encoded vector. For example, let us assume you have 3 classes of genres - comedy, romantic and horror. There are many ways to make it and Scikit-learn has a method which makes it very easy which I show below.

>>> from sklearn.preprocessing import MultiLabelBinarizer
>>> mlb = MultiLabelBinarizer()
>>> y = mlb.fit_transform([[0,2],[1]])
array([[1, 0, 1],
   [0, 1, 0]])

I initially considered using a softmax layer as my output layer, but since a movie can have multiple genre labels, how should my output be?

This array is the right format to pass to the keras model.fit (assuming that is the method you want it to be pass it to)a simple Keras example I suggest.

>>> from keras.models import Sequential
>>> from keras.layers import Dense, Activation

>>> model = Sequential([
    Dense(32, input_dim=784),
    Activation('relu'),
    Dense(10),
    Activation('sigmoid'),
    ])
>>> model.compile(optimizer='rmsprop', loss='categorical_crossentropy')
>>> model.fit(X_train, y_train)

Refer this for cost function relatedmore info. I used sigmoid because it is better for multilabel classification.

Your output layer should be a one hot encoded vector. For example, let us assume you have 3 classes of genres - comedy, romantic and horror. There are many ways to make it and Scikit-learn has a method which makes it very easy which I show below.

>>> from sklearn.preprocessing import MultiLabelBinarizer
>>> mlb = MultiLabelBinarizer()
>>> y = mlb.fit_transform([[0,2],[1]])
array([[1, 0, 1],
   [0, 1, 0]])

This array is the right format to pass to the keras model.fit (assuming that is the method you want it to be pass it to).

Refer this for cost function related info.

how would I have to format my data to make it work with Keras?

Your training labels in the output layer should be a one hot encoded vector. For example, let us assume you have 3 classes of genres - comedy, romantic and horror. There are many ways to make it and Scikit-learn has a method which makes it very easy which I show below.

>>> from sklearn.preprocessing import MultiLabelBinarizer
>>> mlb = MultiLabelBinarizer()
>>> y = mlb.fit_transform([[0,2],[1]])
array([[1, 0, 1],
   [0, 1, 0]])

I initially considered using a softmax layer as my output layer, but since a movie can have multiple genre labels, how should my output be?

This is a simple Keras example I suggest.

>>> from keras.models import Sequential
>>> from keras.layers import Dense, Activation

>>> model = Sequential([
    Dense(32, input_dim=784),
    Activation('relu'),
    Dense(10),
    Activation('sigmoid'),
    ])
>>> model.compile(optimizer='rmsprop', loss='categorical_crossentropy')
>>> model.fit(X_train, y_train)

Refer this for more info. I used sigmoid because it is better for multilabel classification.

added 189 characters in body
Source Link
Hima Varsha
  • 2.4k
  • 15
  • 34
Loading
Source Link
Hima Varsha
  • 2.4k
  • 15
  • 34
Loading