Python Forum
NoneType' object is not subscriptable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NoneType' object is not subscriptable
#1
Can someone look at the issue. the problem lies at last two line 'x_val =x_train[:10000]
partial_x_train = x_train[10000:]' Though I'm not using putting other libraries, however, assuming that they are working fine and as I said the real problem exists in the end.

from keras.datasets import imdb
from keras import optimizers
from keras import losses
from keras import metrics
import numpy as np
(train_data, train_lables), (test_data, test_lables)=imdb.load_data(num_words=10000)
def vectorize_sequences(sequences, dimension=10000):
    results = np.zeros((len(sequences), dimension))
    print(results)
    for i, sequence in enumerate(sequences):
        results[i, sequence] = 1.
    return
x_train = vectorize_sequences(train_data)
x_test = vectorize_sequences(test_data)
y_train = np.asarray(train_lables).astype('float32')
y_test = np.asarray(test_lables).astype('float32') 
model = models.Sequential()
model.add(layers.Dense(16, activation='relu', input_shape=(10000,)))
model.add(layers.Dense(16, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy'])
x_val =x_train[:10000]
partial_x_train = x_train[10000:]
Reply
#2
your vectorize_sequences function does not return anything (i.e. just return statement on line 12) so it returns None
I guess you want to return results
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Sep-10-2019, 08:20 PM)buran Wrote: your vectorize_sequences function does not return anything (i.e. just return statement on line 12) so it returns None I guess you want to return results
I agreed Burhan, Can you just let me know,how to tackle with that, I mean how solve issue. Thanks in advance
Reply
#4
(Sep-10-2019, 08:20 PM)buran Wrote: I guess you want to return results
As I said I think you want to return results

line 12:
return results
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(Sep-11-2019, 12:17 PM)buran Wrote:
(Sep-10-2019, 08:20 PM)buran Wrote: I guess you want to return results
As I said I think you want to return results line 12:
return results
Bro Still in problem, and got this error "NameError Traceback (most recent call last)
cell_name in async-def-wrapper()
NameError: name 'sequences' is not defined"
Now WTH is that
Reply
#6
always post the full traceback and relevant code.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
(Sep-12-2019, 07:33 PM)buran Wrote: This is the same code, and the error I got is "NameError Traceback (most recent call last)
cell_name in async-def-wrapper()
NameError: name 'sequences' is not defined"
from keras.datasets import imdb
from keras import optimizers
from keras import losses
from keras import metrics
import numpy as np
(train_data, train_lables), (test_data, test_lables)=imdb.load_data(num_words=10000)
def vectorize_sequences(sequences, dimension=10000):
    results = np.zeros((len(sequences), dimension))
    print(results)
    for i, sequence in enumerate(sequences):
        results[i, sequence] = 1.
    return results
x_train = vectorize_sequences(train_data)
x_test = vectorize_sequences(test_data)
y_train = np.asarray(train_lables).astype('float32')
y_test = np.asarray(test_lables).astype('float32')
model = models.Sequential()
model.add(layers.Dense(16, activation='relu', input_shape=(10000,)))
model.add(layers.Dense(16, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy'])
x_val =x_train[:10000]
partial_x_train = x_train[10000:]

Reply
#8
normaly traceback would have the line numbers
and I don't see anywhere in your code cell_name in async-def-wrapper()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
(Sep-12-2019, 07:41 PM)buran Wrote: normaly traceback would have the line numbers and I don't see anywhere in your code cell_name in async-def-wrapper()
This is the problem bro! I couldn't find any line number just this error pop up but the problem still lies in this section as I'm using jupyter thats why I know
def vectorize_sequences(sequences, dimension=10000):
results = np.zeros((len(sequences), dimension))
print(results)
for i, sequence in enumerate(sequences):
results[i, sequence] = 1.
return results
Reply
#10
This error means that you attempted to index an object that doesn’t have that functionality. You might have noticed that the method sort() that only modify the list have no return value printed – they return the default None. ‘NoneType’ object is not subscriptable is the one thrown by python when you use the square bracket notation object[key] where an object doesn’t define the getitem method . This is a design principle for all mutable data structures in Python. You can reproduce TypeError that you get in your code if you try this at the Python command line:

None[0]

None has a special status in Python. It is a favorite baseline value because many algorithms treat it as an exceptional value, used in many places in the language and library to represent the absence of some other value .
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  AttributeError: 'NoneType' object has no attribute 'all' synthex 2 1,957 Mar-07-2019, 11:11 AM
Last Post: synthex
  TypeError: 'method' object is not subscriptable ayaz786amd 1 33,634 Nov-24-2018, 11:16 AM
Last Post: j.crater

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020