Code Monkey home page Code Monkey logo

Comments (17)

maxpumperla avatar maxpumperla commented on July 18, 2024

It looks to me that you're using IPython notebooks, which does not work very well with hyperas. The error message you get seems to arise from hyperas trying to interpret the stuff surrounding the cells, not the content.

Can you please try to work with a regular script instead?

from hyperas.

twangnh avatar twangnh commented on July 18, 2024

Thank you, yes, I used IPython console to run the code, following your advice I run the code on regular python console, and this time the error info is different from the previous one, below is the error info:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\SciSoft\WinPython-64bit-2.7.9.4\python-2.7.9.amd64\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 682, in runfile
    execfile(filename, namespace)
  File "C:\SciSoft\WinPython-64bit-2.7.9.4\python-2.7.9.amd64\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)
  File "C:/SciSoft/my_code/untitled0.py", line 78, in <module>
    trials=Trials())
  File "c:\scisoft\winpython-64bit-2.7.9.4\python-2.7.9.amd64\hyperas-\hyperas\optim.py", line 31, in minimize
    best_run = base_minimizer(model, data, algo, max_evals, trials, rseed)
  File "c:\scisoft\winpython-64bit-2.7.9.4\python-2.7.9.amd64\hyperas-\hyperas\optim.py", line 92, in base_minimizer
    model_str = get_hyperopt_model_string(model, data)
  File "c:\scisoft\winpython-64bit-2.7.9.4\python-2.7.9.amd64\hyperas-\hyperas\optim.py", line 66, in get_hyperopt_model_string
    with open(calling_script_file, 'r') as f:
IOError: [Errno 22] invalid mode ('r') or filename: 'C:\\SciSoft\\my_code\\<stdin>'

from hyperas.

maxpumperla avatar maxpumperla commented on July 18, 2024

Don't work interactively with a shell, execute a script! Your error message says that stdin (standard console input) is not a file, which it isn't.

from hyperas.

maxpumperla avatar maxpumperla commented on July 18, 2024

i.e. just do python examples/simple.py or whatever you want to execute.

from hyperas.

twangnh avatar twangnh commented on July 18, 2024

I execute this example code by directlly using python example_code\example_1.py, and I got the following meassage:

Unexpected error: <type 'exceptions.SyntaxError'>
Traceback (most recent call last):
  File "untitled0.py", line 80, in <module>
    trials=Trials())
  File "c:\scisoft\winpython-64bit-2.7.9.4\python-2.7.9.amd64\hyperas-\hyperas\o
ptim.py", line 31, in minimize
    best_run = base_minimizer(model, data, algo, max_evals, trials, rseed)
  File "c:\scisoft\winpython-64bit-2.7.9.4\python-2.7.9.amd64\hyperas-\hyperas\o
ptim.py", line 96, in base_minimizer
    from temp_model import keras_fmin_fnct, get_space
  File "C:\SciSoft\my_code\temp_model.py", line 7
    Make sure to have every relevant import statement included here and return d
ata as
            ^
SyntaxError: invalid syntax

Below is example_1.py:

from __future__ import print_function
from hyperopt import Trials, STATUS_OK, tpe
from hyperas import optim
from hyperas.distributions import choice, uniform, conditional
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation

def data():
    '''
    Data providing function:

    Make sure to have every relevant import statement included here and return data as
    used in model function below. This function is separated from model() so that hyperopt
    won't reload data for each evaluation run.
    '''
    from keras.datasets import mnist
    from keras.utils import np_utils
    (X_train, y_train), (X_test, y_test) = mnist.load_data()
    X_train = X_train.reshape(60000, 784)
    X_test = X_test.reshape(10000, 784)
    X_train = X_train.astype('float32')
    X_test = X_test.astype('float32')
    X_train /= 255
    X_test /= 255

    nb_classes = 10
    Y_train = np_utils.to_categorical(y_train, nb_classes)
    Y_test = np_utils.to_categorical(y_test, nb_classes)
    return X_train, Y_train, X_test, Y_test


def model(X_train, Y_train, X_test, Y_test):
    '''
    Model providing function:

    Create Keras model with double curly brackets dropped-in as needed.
    Return value has to be a valid python dictionary with two customary keys:
        - loss: Specify a numeric evaluation metric to be minimized
        - status: Just use STATUS_OK and see hyperopt documentation if not feasible
    The last one is optional, though recommended, namely:
        - model: specify the model just created so that we can later use it again.
    '''


    model = Sequential()
    model.add(Dense(512, input_shape=(784,)))
    model.add(Activation('relu'))
    model.add(Dropout({{uniform(0, 1)}}))
    model.add(Dense({{choice([256, 512, 1024])}}))
    model.add(Activation({{choice(['relu', 'sigmoid'])}}))
    model.add(Dropout({{uniform(0, 1)}}))

    # If we choose 'four', add an additional fourth layer
    if conditional({{choice(['three', 'four'])}}) == 'four':
        model.add(Dense(100))
        # We can also choose between complete sets of layers
        model.add({{choice([Dropout(0.5), Activation('linear')])}})
        model.add(Activation('relu'))

    model.add(Dense(10))
    model.add(Activation('softmax'))

    model.compile(loss='categorical_crossentropy', optimizer={{choice(['rmsprop', 'adam', 'sgd'])}})

    model.fit(X_train, Y_train,
              batch_size={{choice([64, 128])}},
              nb_epoch=1,
              show_accuracy=True,
              verbose=2,
              validation_data=(X_test, Y_test))
    score, acc = model.evaluate(X_test, Y_test, show_accuracy=True, verbose=0)
    print('Test accuracy:', acc)
    return {'loss': -acc, 'status': STATUS_OK, 'model': model}

if __name__ == '__main__':
    best_run, best_model = optim.minimize(model=model,
                                          data=data,
                                          algo=tpe.suggest,
                                          max_evals=5,
                                          trials=Trials())
    X_train, Y_train, X_test, Y_test = data()
    print("Evalutation of best performing model:")
    print(best_model.evaluate(X_test, Y_test))

from hyperas.

twangnh avatar twangnh commented on July 18, 2024

It seems that my code has some problem, I recopied the code from official webpage, and ran it again, now it cames with the following error info:

Loading data...
Unexpected error: <type 'exceptions.TypeError'>
Traceback (most recent call last):
  File "untitled0.py", line 65, in <module>
    trials=Trials())
  File "c:\scisoft\winpython-64bit-2.7.9.4\python-2.7.9.amd64\hyperas-\hyperas\o
ptim.py", line 31, in minimize
    best_run = base_minimizer(model, data, algo, max_evals, trials, rseed)
  File "c:\scisoft\winpython-64bit-2.7.9.4\python-2.7.9.amd64\hyperas-\hyperas\o
ptim.py", line 96, in base_minimizer
    from temp_model import keras_fmin_fnct, get_space
  File "C:\SciSoft\my_code\temp_model.py", line 19, in <module>
    (X_train, y_train), (X_test, y_test) = imdb.load_data(nb_words=max_features,
 test_split=0.2)
  File "C:\SciSoft\WinPython-64bit-2.7.9.4\python-2.7.9.amd64\lib\site-packages\
keras\datasets\imdb.py", line 24, in load_data
    np.random.shuffle(X)
  File "mtrand.pyx", line 4607, in mtrand.RandomState.shuffle (numpy\random\mtra
nd\mtrand.c:28561)
  File "mtrand.pyx", line 4610, in mtrand.RandomState.shuffle (numpy\random\mtra
nd\mtrand.c:28505)
TypeError: 'tuple' object does not support item assignment

from hyperas.

maxpumperla avatar maxpumperla commented on July 18, 2024

Well, that last one seems to be related to numpy and loading imdb data, not hyperas.

Are you able to run simple.py from the examples?

from hyperas.

twangnh avatar twangnh commented on July 18, 2024

Thank you!, I ran the simple.py code, it finally works properly, then I edited it to optimize my own model, but strange problem came up as follows:

Unexpected error: <type 'exceptions.SyntaxError'>
Traceback (most recent call last):
  File "untitled2.py", line 214, in <module>
    trials=Trials())
  File "c:\scisoft\winpython-64bit-2.7.9.4\python-2.7.9.amd64\hyperas-\hyperas\o
ptim.py", line 31, in minimize
    best_run = base_minimizer(model, data, algo, max_evals, trials, rseed)
  File "c:\scisoft\winpython-64bit-2.7.9.4\python-2.7.9.amd64\hyperas-\hyperas\o
ptim.py", line 96, in base_minimizer
    from temp_model import keras_fmin_fnct, get_space
  File "C:\SciSoft\my_code\temp_model.py", line 11
    Make sure to have every relevant import statement included here and return d
ata as
            ^
SyntaxError: invalid syntax

I find that in my code folder, every time after I ran my own code, it would creat a file temp_model.py and then, the error info came up, I opened temp_model.py, following is it, it's just a little different from my original code, which I pasted latter, I think there is something wrong with temp_model.py

from __future__ import print_function
from hyperopt import Trials, STATUS_OK, tpe
from hyperas import optim
from hyperas.distributions import choice, uniform
import numpy as np
from keras.models import Sequential
from keras.optimizers import SGD
from keras.layers import Dense, Activation, Dropout, TimeDistributed
from keras.layers import SimpleRNN
from keras.layers.core import Masking
Make sure to have every relevant import statement included here and return data as
from hyperopt import fmin, tpe, hp, STATUS_OK, Trials
from hyperas.distributions import conditional

'''
Data providing function:

Make sure to have every relevant import statement included here and 
used in model function below. This function is separated from model() so that hyperopt
won't reload data for each evaluation run.
'''

####################################################
####################################################
#path = get_file('nietzsche.txt', origin="https://s3.amazonaws.com/text-datasets/nietzsche.txt")
#text = open('C:\\SciSoft\\my_code\\train.txt').read().lower()
data = open('duduniu.txt', 'r').readlines()[0:8000]
data_add=open('csdn.txt', 'r').readlines()[0:8000]
data= data+ data_add
print('corpus length:', len(data))
mylist=get_list(data)
mylist=list_clean(mylist,MAXLEN,MINLEN)
#print mylist
data_after=get_row_data(mylist)
#mylist=pad_mylist(mylist,MAXLEN)
chars = set(data_after)

print('total chars:', len(chars))
char_indices = dict((c, i) for i, c in enumerate(chars))

 indices_char = dict((i, c) for i, c in enumerate(chars))

# cut the text in semi-redundant sequences of maxlen characters


print('nb sequences:', len(mylist))

print('Vectorization...')
X_train = np.zeros((len(mylist), MAXLEN, len(chars)), dtype=np.bool)
Y_train = np.zeros((len(mylist), MAXLEN, len(chars)), dtype=np.bool)

for i, sentence in enumerate(mylist):
    for t in range(len(sentence)-1):
        X_train[i, t, char_indices[sentence[t]]] = 1
        Y_train[i, t, char_indices[sentence[t+1]]] = 1

data = open('178.txt', 'r').readlines()[0:1000]
mylist_test=get_list(data)
mylist_test=list_clean(mylist_test,MAXLEN,MINLEN,chars,fit_for_model=True)
 anll=[]
##################
#mylist=pad_mylist(mylist,MAXLEN)
X_test = np.zeros((len(mylist_test), MAXLEN, len(chars)), dtype=np.bool)
Y_test = np.zeros((len(mylist_test), MAXLEN, len(chars)), dtype=np.bool)

for i, sentence in enumerate(mylist_test):
    for t in range(len(sentence)-1):
        X_test[i, t, char_indices[sentence[t]]] = 1
        Y_test[i, t, char_indices[sentence[t+1]]] = 1


def keras_fmin_fnct(space):

    '''
    Model providing function:

    Create Keras model with double curly brackets dropped-in as needed.
    Return value has to be a valid python dictionary with two customary keys:
        - loss: Specify a numeric evaluation metric to be minimized
        - status: Just use STATUS_OK and see hyperopt documentation if not feasible
    The last one is optional, though recommended, namely:
        - model: specify the model just created so that we can later use it again.
    '''


    model = Sequential()
    model.add(Masking(mask_value=0., input_shape=(None, 84)))
    model.add(SimpleRNN(400, return_sequences=True))
    model.add(Dropout(space['Dropout']))
    model.add(TimeDistributed(Dense(84)))
    model.add(Activation('softmax'))

    sgd = SGD(lr=0.10, decay=3e-5, momentum=0.9, nesterov=True)
    model.compile(loss='categorical_crossentropy', optimizer=sgd,metrics=['accuracy'])

    model.fit(X_train, Y_train,
              batch_size=space['batch_size'],
              nb_epoch=1,
              show_accuracy=True,
              verbose=2,
              validation_data=(X_test, Y_test))
    score, acc = model.evaluate(X_test, Y_test, verbose=0)
    print('Test accuracy:', acc)
    return {'loss': -acc, 'status': STATUS_OK, 'model': model}


def get_space():
    return {
        'Dropout': hp.uniform('Dropout', 0, 1),
        'batch_size': hp.choice('batch_size', [64, 128]),
    }

Here is my own code:

from __future__ import print_function
from hyperopt import Trials, STATUS_OK, tpe
from hyperas import optim
from hyperas.distributions import choice, uniform
import numpy as np
from keras.models import Sequential
from keras.optimizers import SGD
from keras.layers import Dense, Activation, Dropout, TimeDistributed
from keras.layers import SimpleRNN
from keras.layers.core import Masking

def detect_start1(inputs):
    i=0
    while(inputs[i]==' '):
        i+=1
    while(inputs[i]!=' '):
        i+=1
    return i

def detect_start2(inputs):
    i=0
    while(inputs[i]!=' '):
        i+=1
    return i


def get_list(inputs):
    mylist=[];t=0
    if inputs[0][0]==' ':
        while(t<len(inputs)):
            s=detect_start1(inputs[t])+1
            mylist.append(inputs[t][s:])
            t+=1
    else:
        while(t<len(inputs)):
            s=detect_start2(inputs[t])+1
            mylist.append(inputs[t][s:])
            t+=1
    return mylist


def get_row_data(mylist):
    row_data='';t=0
    while(t<len(mylist)):
        row_data+=mylist[t]
        t+=1
    return row_data

def find_first_smaller(nums, target):
    if not nums:
        return 0
    low = 0
    high = len(nums) - 1
    while low <= high:
        mid = (low + high) // 2
        if nums[mid] == target:
            return mid
        elif nums[mid] > target:
            high = mid - 1
        else:
            low = mid + 1
    return low     

def list_clean(mylist,maxlen=None,minlen=None,chars=None,fit_for_model=False):
    length=len(mylist);t=0
    if maxlen:
        while(t<length):
            if len(mylist[t])>maxlen or len(mylist[t])<minlen:
                print (t)
                mylist.remove(mylist[t])
                t-=1
                length-=1
            t+=1
    t=0
    while(t<length):
        for k in range(len(mylist[t])):
#            if not (47<ord(mylist[t][k])<58 or 96<ord(mylist[t][k])<123 or ord(mylist[t][k])==10):
            if not(32<ord(mylist[t][k])<126 or ord(mylist[t][k])==10):
                print (t)
                mylist.remove(mylist[t])
                t-=1
                length-=1
                break
        t+=1
    if fit_for_model==True:
        t=0
        while(t<length):
            for k in range(len(mylist[t])):
                if mylist[t][k] not in chars:
                    print (t)
                    mylist.remove(mylist[t])
                    t-=1
                    length-=1
                    break
            t+=1        
    return mylist

def pad_mylist(mylist,maxlen):
    for t in range(len(mylist)):
        if len(mylist[t])<maxlen:
            mylist[t]+='\n'*(maxlen-len(mylist[t]))
    return mylist

def calculate_anll(plot_list,x_range=80):
    sum=0;x=1
    while(plot_list[x]<0.8):
        x+=1
        sum+=(plot_list[x]-plot_list[x-1])*x
    return sum
MAXLEN=12
MINLEN=4


def data():
    '''
    Data providing function:

    Make sure to have every relevant import statement included here and return data as
    used in model function below. This function is separated from model() so that hyperopt
    won't reload data for each evaluation run.
    '''

    ####################################################
    ####################################################
    #path = get_file('nietzsche.txt', origin="https://s3.amazonaws.com/text-datasets/nietzsche.txt")
    #text = open('C:\\SciSoft\\my_code\\train.txt').read().lower()
    data = open('duduniu.txt', 'r').readlines()[0:8000]
    data_add=open('csdn.txt', 'r').readlines()[0:8000]
    data= data+ data_add
    print('corpus length:', len(data))
    mylist=get_list(data)
    mylist=list_clean(mylist,MAXLEN,MINLEN)
    #print mylist
    data_after=get_row_data(mylist)
    #mylist=pad_mylist(mylist,MAXLEN)
    chars = set(data_after)

    print('total chars:', len(chars))
    char_indices = dict((c, i) for i, c in enumerate(chars))

#    indices_char = dict((i, c) for i, c in enumerate(chars))

    # cut the text in semi-redundant sequences of maxlen characters


    print('nb sequences:', len(mylist))

    print('Vectorization...')
    X_train = np.zeros((len(mylist), MAXLEN, len(chars)), dtype=np.bool)
    Y_train = np.zeros((len(mylist), MAXLEN, len(chars)), dtype=np.bool)

    for i, sentence in enumerate(mylist):
        for t in range(len(sentence)-1):
            X_train[i, t, char_indices[sentence[t]]] = 1
            Y_train[i, t, char_indices[sentence[t+1]]] = 1

    data = open('178.txt', 'r').readlines()[0:1000]
    mylist_test=get_list(data)
    mylist_test=list_clean(mylist_test,MAXLEN,MINLEN,chars,fit_for_model=True)
#    anll=[]
    ##################
    #mylist=pad_mylist(mylist,MAXLEN)
    X_test = np.zeros((len(mylist_test), MAXLEN, len(chars)), dtype=np.bool)
    Y_test = np.zeros((len(mylist_test), MAXLEN, len(chars)), dtype=np.bool)

    for i, sentence in enumerate(mylist_test):
        for t in range(len(sentence)-1):
            X_test[i, t, char_indices[sentence[t]]] = 1
            Y_test[i, t, char_indices[sentence[t+1]]] = 1
    return X_train, Y_train, X_test, Y_test


def model(X_train, Y_train, X_test, Y_test):
    '''
    Model providing function:

    Create Keras model with double curly brackets dropped-in as needed.
    Return value has to be a valid python dictionary with two customary keys:
        - loss: Specify a numeric evaluation metric to be minimized
        - status: Just use STATUS_OK and see hyperopt documentation if not feasible
    The last one is optional, though recommended, namely:
        - model: specify the model just created so that we can later use it again.
    '''


    model = Sequential()
    model.add(Masking(mask_value=0., input_shape=(None, 84)))
    #model.add(LSTM(300, return_sequences=True, input_shape=(None, len(chars))))
    model.add(SimpleRNN(400, return_sequences=True))
    model.add(Dropout({{uniform(0, 1)}}))
    #model.add(LSTM(256, return_sequences=True))
    #model.add(Dropout(0.2))
    model.add(TimeDistributed(Dense(84)))
    model.add(Activation('softmax'))

    sgd = SGD(lr=0.10, decay=3e-5, momentum=0.9, nesterov=True)
    model.compile(loss='categorical_crossentropy', optimizer=sgd,metrics=['accuracy'])

    model.fit(X_train, Y_train,
              batch_size={{choice([64, 128])}},
              nb_epoch=1,
              show_accuracy=True,
              verbose=2,
              validation_data=(X_test, Y_test))
    score, acc = model.evaluate(X_test, Y_test, verbose=0)
    print('Test accuracy:', acc)
    return {'loss': -acc, 'status': STATUS_OK, 'model': model}

if __name__ == '__main__':
    best_run, best_model = optim.minimize(model=model,
                                          data=data,
                                          algo=tpe.suggest,
                                          max_evals=5,
                                          trials=Trials())
    X_train, Y_train, X_test, Y_test = data()
    print("Evalutation of best performing model:")
    print(best_model.evaluate(X_test, Y_test))

from hyperas.

maxpumperla avatar maxpumperla commented on July 18, 2024

Could you please try to remove multi-line comments and see if it works? I.e. remove stuff like

    '''
    Data providing function:

    Make sure to have every relevant import statement included here and return data as
    used in model function below. This function is separated from model() so that hyperopt
    won't reload data for each evaluation run.
    '''

    ####################################################
    ####################################################
    #path = get_file('nietzsche.txt', origin="https://s3.amazonaws.com/text-datasets/nietzsche.txt")
    #text = open('C:\\SciSoft\\my_code\\train.txt').read().lower()

from hyperas.

maxpumperla avatar maxpumperla commented on July 18, 2024

Hyperas, at its core, is a string parser. Apparently something goes wrong in your case due to comments, at least that's what I suspect from:

    Make sure to have every relevant import statement included here and return data as
            ^
SyntaxError: invalid syntax

This suggests that python tries to execute this line as python code, which it shouldn't. If you can confirm this and it works, I'll have to fix that potential bug.

from hyperas.

twangnh avatar twangnh commented on July 18, 2024

Thank you! And sorry for the delayed reply, I was out and I just came back. You are right, after I removed all the comments, it works properly, I find that we should not write any code between import lines and data() function, and best remove any function in data() that have return in it, because in generated temp_mode.py, the return will be removed, and this can cause some problem. I still have some problem that is confusing, after I ran my edited code, although the problems with temp_model.py have all been solved, another problem came up as follows:

C:\SciSoft\WinPython-64bit-2.7.9.4\python-2.7.9.amd64\lib\site-packages\keras\mo
dels.py:388: UserWarning: The "show_accuracy" argument is deprecated, instead yo
u should pass the "accuracy" metric to the model at compile time:
`model.compile(optimizer, loss, metrics=["accuracy"])`
  warnings.warn('The "show_accuracy" argument is deprecated, '
Train on 1958 samples, validate on 88 samples
Epoch 1/1
Traceback (most recent call last):
  File "untitled2.py", line 86, in <module>
    trials=Trials())
  File "c:\scisoft\winpython-64bit-2.7.9.4\python-2.7.9.amd64\hyperas-\hyperas\o
ptim.py", line 31, in minimize
    best_run = base_minimizer(model, data, algo, max_evals, trials, rseed)
  File "c:\scisoft\winpython-64bit-2.7.9.4\python-2.7.9.amd64\hyperas-\hyperas\o
ptim.py", line 111, in base_minimizer
    rseed=rseed)
  File "C:\SciSoft\WinPython-64bit-2.7.9.4\python-2.7.9.amd64\lib\site-packages\
hyperopt\fmin.py", line 334, in fmin
    rval.exhaust()
  File "C:\SciSoft\WinPython-64bit-2.7.9.4\python-2.7.9.amd64\lib\site-packages\
hyperopt\fmin.py", line 294, in exhaust
    self.run(self.max_evals - n_done, block_until_done=self.async)
  File "C:\SciSoft\WinPython-64bit-2.7.9.4\python-2.7.9.amd64\lib\site-packages\
hyperopt\fmin.py", line 268, in run
    self.serial_evaluate()
  File "C:\SciSoft\WinPython-64bit-2.7.9.4\python-2.7.9.amd64\lib\site-packages\
hyperopt\fmin.py", line 187, in serial_evaluate
    result = self.domain.evaluate(spec, ctrl)
  File "C:\SciSoft\WinPython-64bit-2.7.9.4\python-2.7.9.amd64\lib\site-packages\
hyperopt\fmin.py", line 114, in evaluate
    rval = self.fn(pyll_rval)
  File "C:\SciSoft\my_code\temp_model.py", line 70, in keras_fmin_fnct
  File "C:\SciSoft\WinPython-64bit-2.7.9.4\python-2.7.9.amd64\lib\site-packages\
keras\models.py", line 405, in fit
    sample_weight=sample_weight)
  File "C:\SciSoft\WinPython-64bit-2.7.9.4\python-2.7.9.amd64\lib\site-packages\
keras\engine\training.py", line 1046, in fit
    callback_metrics=callback_metrics)
  File "C:\SciSoft\WinPython-64bit-2.7.9.4\python-2.7.9.amd64\lib\site-packages\
keras\engine\training.py", line 784, in _fit_loop
    outs = f(ins_batch)
  File "C:\SciSoft\WinPython-64bit-2.7.9.4\python-2.7.9.amd64\lib\site-packages\
keras\backend\theano_backend.py", line 507, in __call__
    return self.function(*inputs)
  File "C:\SciSoft\WinPython-64bit-2.7.9.4\python-2.7.9.amd64\lib\site-packages\
theano\compile\function_module.py", line 871, in __call__
    storage_map=getattr(self.fn, 'storage_map', None))
  File "C:\SciSoft\WinPython-64bit-2.7.9.4\python-2.7.9.amd64\lib\site-packages\
theano\gof\link.py", line 314, in raise_with_op
    reraise(exc_type, exc_value, exc_trace)
  File "C:\SciSoft\WinPython-64bit-2.7.9.4\python-2.7.9.amd64\lib\site-packages\
theano\compile\function_module.py", line 859, in __call__
    outputs = self.fn()
ValueError: total size of new array must be unchanged
Apply node that caused the error: Reshape{2}(Elemwise{mul,no_inplace}.0, TensorC
onstant{[-1 84]})
Toposort index: 80
Inputs types: [TensorType(float32, 3D), TensorType(int32, vector)]
Inputs shapes: [(2L, 12L, 76L), (2L,)]
Inputs strides: [(3648L, 304L, 4L), (4L,)]
Inputs values: ['not shown', array([-1, 84])]
Outputs clients: [[Dot22(Reshape{2}.0, simplernn_1_W), InplaceDimShuffle{1,0}(Re
shape{2}.0)]]

Backtrace when the node is created(use Theano flag traceback.limit=N to make it
longer):
  File "C:\SciSoft\WinPython-64bit-2.7.9.4\python-2.7.9.amd64\lib\site-packages\
keras\models.py", line 142, in add
    output_tensor = layer(self.outputs[0])
  File "C:\SciSoft\WinPython-64bit-2.7.9.4\python-2.7.9.amd64\lib\site-packages\
keras\engine\topology.py", line 485, in __call__
    self.add_inbound_node(inbound_layers, node_indices, tensor_indices)
  File "C:\SciSoft\WinPython-64bit-2.7.9.4\python-2.7.9.amd64\lib\site-packages\
keras\engine\topology.py", line 543, in add_inbound_node
    Node.create_node(self, inbound_layers, node_indices, tensor_indices)
  File "C:\SciSoft\WinPython-64bit-2.7.9.4\python-2.7.9.amd64\lib\site-packages\
keras\engine\topology.py", line 148, in create_node
    output_tensors = to_list(outbound_layer.call(input_tensors[0], mask=input_ma
sks[0]))
  File "C:\SciSoft\WinPython-64bit-2.7.9.4\python-2.7.9.amd64\lib\site-packages\
keras\layers\recurrent.py", line 219, in call
    preprocessed_input = self.preprocess_input(x)
  File "C:\SciSoft\WinPython-64bit-2.7.9.4\python-2.7.9.amd64\lib\site-packages\
keras\layers\recurrent.py", line 348, in preprocess_input
    timesteps)
  File "C:\SciSoft\WinPython-64bit-2.7.9.4\python-2.7.9.amd64\lib\site-packages\
keras\layers\recurrent.py", line 32, in time_distributed_dense
    x = K.reshape(x, (-1, input_dim))
  File "C:\SciSoft\WinPython-64bit-2.7.9.4\python-2.7.9.amd64\lib\site-packages\
keras\backend\theano_backend.py", line 283, in reshape
    return T.reshape(x, shape)

HINT: Use the Theano flag 'exception_verbosity=high' for a debugprint and storag
e map footprint of this apply node.

BTW, my edited code is:

from __future__ import print_function
from hyperopt import Trials, STATUS_OK, tpe
from hyperas import optim
from hyperas.distributions import choice, uniform

import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout, TimeDistributed
from keras.optimizers import SGD

from keras.layers import SimpleRNN
from keras.layers.core import Masking
from my_func import get_list 
from my_func import list_clean 
from my_func import get_row_data



def data():
    MAXLEN=12
    MINLEN=4  

    data = open('duduniu.txt', 'r').readlines()[0:1000]
    data_add=open('csdn.txt', 'r').readlines()[0:1000]
    data= data+ data_add
    print('corpus length:', len(data))
    mylist=get_list(data)
    mylist=list_clean(mylist,MAXLEN,MINLEN)
    data_after=get_row_data(mylist)
    chars = set(data_after)
    print('total chars:', len(chars))
    char_indices = dict((c, i) for i, c in enumerate(chars))    
    print('nb sequences:', len(mylist))    
    print('Vectorization...')
    X_train = np.zeros((len(mylist), MAXLEN, len(chars)), dtype=np.bool)
    Y_train = np.zeros((len(mylist), MAXLEN, len(chars)), dtype=np.bool)

    for i, sentence in enumerate(mylist):
        for t in range(len(sentence)-1):
            X_train[i, t, char_indices[sentence[t]]] = 1
            Y_train[i, t, char_indices[sentence[t+1]]] = 1

    data = open('178.txt', 'r').readlines()[0:100]
    mylist_test=get_list(data)
    mylist_test=list_clean(mylist_test,MAXLEN,MINLEN,chars,fit_for_model=True)
    X_test = np.zeros((len(mylist_test), MAXLEN, len(chars)), dtype=np.bool)
    Y_test = np.zeros((len(mylist_test), MAXLEN, len(chars)), dtype=np.bool)

    for i, sentence in enumerate(mylist_test):
        for t in range(len(sentence)-1):
            X_test[i, t, char_indices[sentence[t]]] = 1
            Y_test[i, t, char_indices[sentence[t+1]]] = 1
    return X_train, Y_train, X_test, Y_test


def model(X_train, Y_train, X_test, Y_test):

    model = Sequential()
    model.add(Masking(mask_value=0., input_shape=(None, 84)))
    #model.add(LSTM(300, return_sequences=True, input_shape=(None, len(chars))))
    model.add(SimpleRNN(400, return_sequences=True))
    model.add(Dropout({{uniform(0, 1)}}))
    #model.add(LSTM(256, return_sequences=True))
    #model.add(Dropout(0.2))
    model.add(TimeDistributed(Dense(84)))
    model.add(Activation('softmax'))

    sgd = SGD(lr=0.10, decay=3e-5, momentum=0.9, nesterov=True)
    model.compile(loss='categorical_crossentropy', optimizer=sgd,metrics=['accuracy'])

    model.fit(X_train, Y_train,
              batch_size={{choice([2, 4])}},
              nb_epoch=1,
              show_accuracy=True,
              verbose=2,
              validation_data=(X_test, Y_test))
    score, acc = model.evaluate(X_test, Y_test, verbose=0)
    print('Test accuracy:', acc)
    return {'loss': -acc, 'status': STATUS_OK, 'model': model}

if __name__ == '__main__':
    best_run, best_model = optim.minimize(model=model,
                                          data=data,
                                          algo=tpe.suggest,
                                          max_evals=5,
                                          trials=Trials())
    X_train, Y_train, X_test, Y_test = data()
    print("Evalutation of best performing model:")
    print(best_model.evaluate(X_test, Y_test))

from hyperas.

maxpumperla avatar maxpumperla commented on July 18, 2024

Now that's a Theano error. Apparently TimeDistributedDense is not able to reshape the input, certainly not an issue with hyperas.

Look, I really want to help, but can I propose to close this issue and maybe open another one for the problem with comments?

from hyperas.

maxpumperla avatar maxpumperla commented on July 18, 2024

p.s. I'm offline for the next 3 weeks, so I can't patch anything until then.

from hyperas.

twangnh avatar twangnh commented on July 18, 2024

OK, I see. Thank you very much for helping me!

from hyperas.

twangnh avatar twangnh commented on July 18, 2024

BTW, so we cannot use shell to run the optimization code and must execute it directly by cmd, right?

from hyperas.

maxpumperla avatar maxpumperla commented on July 18, 2024

Sure, yes, maybe we should put that into the readme! Thanks for digging into hyperas.

from hyperas.

twangnh avatar twangnh commented on July 18, 2024

I see, thank you for helping me!

from hyperas.

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.