Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'SparseTensor' object is not subscriptable keras

Following my Studies in Machine learning I am now in the Neural network, I have an assignment - text Classification - using Neural Network.

In the following, I am showing what I have so far

  1. Process Data
  2. Counter Vectorizer

Now I'm trying to compile the NN However I am receiving the following error

TypeError: 'SparseTensor' object is not subscriptable Traceback (most recent call last): File "/opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/script_ops.py", line 242, in call return func(device, token, args) File "/opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/script_ops.py", line 131, in call

My data shape is the following

X_train.shape = (17621, 8014)

type(X_train) = scipy.sparse.csr.csr_matrix

The model

model = Sequential()
model.add(Dense(1015, input_shape=(17621, 8014) , activation = 'relu'))
model.add(Dense(5, activation = 'sigmoid'))
model.add(Dense(1,activation='sigmoid'))
model.compile(loss = 'binary_crossentropy',metrics = ['accuracy'], optimizer = 'adam')
model.fit(x=X_train, y=y_train,epochs=500,batch_size=125,
          validation_data=(X_test,y_test))

in addition, I have 2 more question

  1. What are the differences between input_shape ~ Input_dimmension?
  2. when I add the first Layer. How many perceptrons should I set?
  3. the most important one what Am I doing wrong?

Please feel free to give more suggestion

like image 850
Marvin Raudez Avatar asked Nov 07 '22 04:11

Marvin Raudez


1 Answers

Keras can't work with csr_matrix. Convert to a numpy array.

X_train = X_train.toarray()
like image 118
Dinesh Nagumothu Avatar answered Nov 14 '22 22:11

Dinesh Nagumothu