Loss isn't decreasing in neural network - neural-network

I am implementing a variant of the CNN described by this paper.
My problem is that the loss isn't decreasing and I don't understand why. Same have to be said concerning accuracy(stuck at 0.5 more or less).
This a problem of 2 classes classification. I am using the data from this website:
I suspected the optimizer so I changed it whithout any improvements. I am pretty sure the data I am using is ok because I used it on an LSTM and the classifier was fine.
Here is my code:
from keras.layers import Embedding
from keras.layers import Conv2D
from keras.models import Sequential
from keras.layers import MaxPooling2D
from keras.layers import Reshape
from keras.layers import Flatten
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Input
from keras import backend as K
from keras.models import Model
import tensorflow as tf
from sklearn import preprocessing
import keras
import numpy as np
import pdb
#using multiple filters
config = tf.ConfigProto()
# Don't pre-allocate memory; allocate as-needed
config.gpu_options.allow_growth = True
# Only allow a total of half the GPU memory to be allocated
config.gpu_options.per_process_gpu_memory_fraction = 0.7
# Create a session with the above options specified.
config.gpu_options.per_process_gpu_memory_fraction = 0.65
K.tensorflow_backend.set_session(tf.Session(config=config))
class Classifier():
def __init__(self,Vocab,maxlen=75):
self.model=Sequential()
self.EMBED_DIM=30
self.batch_size=30
self.MAX_SEQUENCE_LENGTH=maxlen
self.nb_labels=2
self.model = Sequential()
self.Vocab=Vocab
def fit(self, X,y):
#pdb.set_trace()
mainIn=Input(shape=(self.MAX_SEQUENCE_LENGTH,), dtype='int32', name='main_input')
x=Embedding(len(self.Vocab)+2,self.EMBED_DIM,input_length=self.MAX_SEQUENCE_LENGTH)(mainIn)
x=Reshape((1,self.MAX_SEQUENCE_LENGTH, self.EMBED_DIM))(x)
x1=Conv2D(128, strides=2,kernel_size=5 ,activation="relu", padding='same')(x)
x1=MaxPooling2D((self.MAX_SEQUENCE_LENGTH-5+1,1),padding='same')(x1)
x1=Flatten()(x1)
x2=Conv2D(128, strides=2, kernel_size=4, activation="sigmoid", padding='same')(x)
x2=MaxPooling2D((self.MAX_SEQUENCE_LENGTH-4+1,1),padding='same')(x2)
x2=Flatten()(x2)
x3=Conv2D(128, strides=2, kernel_size=3, activation="tanh", padding='same')(x)
x3=MaxPooling2D((self.MAX_SEQUENCE_LENGTH-3+1,1),padding='same')(x3)
x3=Flatten()(x3)
combinedX=keras.layers.concatenate([x1,x2,x3],axis=1)
combinedX=Dense(64, activation="relu")(combinedX)
combinedX=Dropout(0.2)(combinedX)
#output=Dense(self.nb_labels, activation="sigmoid")(combinedX)
#output=Dense(2, activation="softmax")(combinedX)
output=Dense(1, activation="sigmoid")(combinedX)
#encoder =preprocessing.LabelEncoder()
#encoder.fit(y)
#encoded_Y = encoder.transform(y)
#labels=keras.utils.to_categorical(encoded_Y, num_classes=2)
labels=y
pdb.set_trace()
inputs2=X
self.model = Model(inputs=mainIn, outputs=output)
# self.model.compile(loss='binary_crossentropy',
# optimizer='adam',
# metrics=['acc'])
self.model.compile(loss='binary_crossentropy',
optimizer='rmsprop',
metrics=['acc'])
self.model.fit(inputs2,labels,epochs=7, batch_size=self.batch_size)
def predict(self, X):
return self.model.predict(np.array(X))
def predict_proba(self, X):
return self.model.predict(np.array(X), self.batch_size)
Here is my code to preprocess the data:
#loading the file
file2=pd.read_csv("Sentiment_Analysis Dataset.csv",error_bad_lines=False)
#splitting into train et test set
from sklearn.model_selection import train_test_split
Text=list(file2.SentimentText)
file2.groupby('Sentiment').count()
train_data,test_data,train_label,test_label=train_test_split(Text, file2.Sentiment, test_size=0.4, random_state=42)
#Buidling the dictionary
vocabDic=dict()
for document in train_data:
document=document.split(" ")
for word in document:
if word not in vocabDic:
vocabDic[word]=len(vocabDic)+1
vocabDic['unk']=len(vocabDic)+1
#coding the documents
def codeDocuments(documents,dictionnary):
documentsArray=list()
for i,document in enumerate(documents):
tempList=list()
document=document.split(" ")
for word in document:
if word in vocabDic:
word=vocabDic[word]
else:
word=vocabDic['unk']
tempList.append(word)
documentsArray.append(tempList)
return np.array(documentsArray)
train_docs=codeDocuments(train_data,vocabDic)
test_docs=codeDocuments(test_data,vocabDic)
#padding the documents
from keras.preprocessing import sequence
maxlen=75
train_set = sequence.pad_sequences(train_docs, maxlen=maxlen)
test_set = sequence.pad_sequences(test_docs, maxlen=maxlen)
#calling the model
model=Classifier(vocabDic,maxlen)
model.fit(train_set[:50000],train_label[:50000])

Related

NotFittedError: Vocabulary not fitted or provided or TypeError: string indices must be integers

Hello everyone, im new hier and want to start learning how machine learning works. So i want to build a machine learning email spam detector in colab, but something seems to be wrong:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import nltk import string
from nltk.corpus import stopwords
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB, GaussianNB
from sklearn import svm
from sklearn.model_selection import GridSearchCV
from google.colab import files
uploaded = files.upload() filename = ('spam.tsv')
content = []
with open(filename, "r") as file_content:
for line in file_content.readlines():
line = line.strip() content.append(line)
spam = line
#print(spam) for testinn if it is working
z = spam['EmailText']
y = spam["Label"]
z_train, z_test,y_train, y_test = train_test_split(z,y,test_size = 0.2)
count_vector = CountVectorizer()
features = count_vector.transform(z_train)
model = svm.SVC()
model.fit(features,y_train)
features_test = count_vector.transform(z_test)
print(model.score(features_test,y_test))`
NotFittedError: Vocabulary not fitted or provided TypeError: string indices must be integers
i tried everything but nothing works really haha
i hope, you can help
thank you

How to export a PyTorch model to MATLAB?

I have created and trained a model in PyTorch, and I would like to be able to use the trained model within a MATLAB program. Based on this post I have been exporting the model to ONNX and then attempting to load the ONNX model in MATLAB. I followed this pytorch tutorial on how to export a model to ONNX.
However, I get this error
Error using nnet.internal.cnn.onnx.importONNXNetwork>iHandleTranslationIssues
Unable to import the network because of the following issues:
1 operator(s) : Unable to create an input layer for ONNX input #1 (with name 'input') because its data format is unknown or not supported as a MATLAB input layer.
If you know the input format, pass it by using the "InputDataFormats" parameter.
The input shape declared in the ONNX file is '(batch_size, 12)'.
1 operator(s) : Unable to create an output layer for ONNX network output #1 (with name 'output') because its data format is unknown or not supported as a MATLAB
output layer. If you know the output format, pass it using the 'OutputDataFormats' parameter.
To import the ONNX network as a dlnetwork, set the 'TargetNetwork' value to 'dlnetwork'.
To import the ONNX network as a layer graph with weights, use importONNXLayers.
To import the ONNX network as a function, use importONNXFunction.
Error in nnet.internal.cnn.onnx.importONNXNetwork (line 37)
iHandleTranslationIssues(translationIssues);
Error in importONNXNetwork (line 113)
Network = nnet.internal.cnn.onnx.importONNXNetwork(modelfile, varargin{:});
Here is a minimal example to create the error
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torch.onnx
class FFNN(nn.Module):
def __init__(self):
super(FFNN, self).__init__()
input_size = 12
self.layer1 = nn.Linear(input_size, 24)
self.layer2 = nn.Linear(24, 24)
self.layer3 = nn.Linear(24, 12)
self.norm1 = nn.BatchNorm1d(12, eps=1e-05, momentum=0.1, affine=False, track_running_stats=True)
self.layer4 = nn.Linear(12, 6)
self.layer5 = nn.Linear(6, 1)
def forward(self, x):
x = F.relu(self.layer1(x))
x = F.relu(self.layer2(x))
x = self.norm1(self.layer3(x))
x = F.relu(self.layer4(x))
out = self.layer5(x)
return out
net = FFNN()
net.eval()
batch_size = 1
input_size = 12
x = torch.randn(batch_size, input_size, requires_grad=True) # random model input for onnx
out = net(x)
input_names = ['x1', 'x2', 'x3', 'x4', 'x5', 'x6', 'x7', 'Qm', 'l1', 'uPre', 'basal', 'dG']
output_names = ['output']
# Export the model
torch.onnx.export(net, # model being run
x, # model input (or a tuple for multiple inputs)
'model.onnx', # where to save the model (can be a file or file-like object)
export_params=True, # store the trained parameter weights inside the model file
opset_version=10, # the ONNX version to export the model to
do_constant_folding=True, # whether to execute constant folding for optimization
input_names = ['input'], # the model's input names
output_names = ['output'], # the model's output names
dynamic_axes={'input' : {0 : 'batch_size'}, # variable length axes
'output' : {0 : 'batch_size'}})
nnMPC = importONNXNetwork("model.onnx"); % produces error
However I can validate the model in ONNX in python and it loads correctly.
So I think the problem is how I am loading it into MATLAB.
(my validation code)
import onnx
import onnxruntime
import numpy as np
onnx_model = onnx.load('model.onnx')
onnx.checker.check_model(onnx_model)
ort_session = onnxruntime.InferenceSession('model.onnx')
def to_numpy(tensor):
return tensor.detach().cpu().numpy() if tensor.requires_grad else tensor.cpu().numpy()
# compute ONNX Runtime output prediction
ort_inputs = {ort_session.get_inputs()[0].name: to_numpy(x)}
ort_outs = ort_session.run(None, ort_inputs)
# compare ONNX Runtime and PyTorch results
np.testing.assert_allclose(to_numpy(out), ort_outs[0], rtol=1e-03, atol=1e-05)
print("Exported model has been tested with ONNXRuntime, and the result looks good!")

MultiOutputRegressor in spark/scala for xgboost

Do we have MultiOutputRegressor as a wrapper for xgboost using spark/scala in distributed way with the help of xgboost4j-spark, my requirement is to implement multiple target prediction.
Please find below the python code snippet for reference.
from sklearn.multioutput import MultiOutputRegressor
from sklearn import ensemble
import xgboost as xgb
xgbr = xgb.XGBRegressor(max_depth=1, eta=0.01, silent=1, subsample= 0.8, reg_lambda = 1.515,
reg_alpha= 0.0017, min_child_weight=7, colsample_bytree=0.85, nthread= 32,
gamma= 0.01, objective='reg:linear',tree_method= 'approx', booster = 'gbtree', n_estimators=100)
X = ["indep_1","indep_2","indep3","indep_4","indep_5","indep_6","indep_7","indep_8"]
Y = ["dep_1","dep_2","dep_3","dep_4"]
model = MultiOutputRegressor(xgbr).fit(X, Y)

Printing confusion matrix to file produces illegal characters

I am classifying a set of images stored as tuples in a csv file.
The confusion matrix that I get on terminal display is correct. But when I write that same conf. matrix to a file, it produces illegal characters (32bit hex).
Here's the code-
from sklearn.metrics import confusion_matrix
import numpy as np
import os
import csv
from sklearn import svm
from sklearn import cross_validation
from sklearn import linear_model
from sklearn.neighbors import KNeighborsClassifier
import matplotlib.pyplot as plt
from sklearn import metrics
import cPickle
def prec(num):
return "%0.5f"%num
outfile = open("output/linear_svm_output.txt","a")
for dim in [20,30,40]:
images=[]
labels=[]
name = str(dim)+"x"+str(dim)+".csv"
with open(name,'r') as file:
reader = csv.reader(file,delimiter=',')
for line in file:
labels.append(line[0])
line=line[2:] # Remove the label
image=[int(pixel) for pixel in line.split(',')]
images.append(np.array(image))
clf = svm.LinearSVC()
print clf
kf = cross_validation.KFold(len(images),n_folds=10,indices=True, shuffle=True, random_state=4)
print "\nDividing dataset using `Kfold()` -:\n\nThe training dataset has been divided into " + str(len(kf)) + " parts\n"
for train, test in kf:
training_images=[]
training_labels=[]
for i in train:
training_images.append(images[i])
training_labels.append(labels[i])
testing_images=[]
testing_labels=[]
for i in test:
testing_images.append(images[i])
testing_labels.append(labels[i])
clf.fit(training_images,training_labels)
predicted = clf.predict(testing_images)
print prec(clf.score(testing_images, testing_labels))
outfile.write(prec(clf.score(testing_images, testing_labels)))
outfile.write(str(clf))
outfile.write(confusion_matrix(testing_labels, predicted))
print confusion_matrix(testing_labels, predicted)
# outfile.write(metrics.classification_report(testing_labels, predicted))
print "\nDividing dataset using `train_test_split()` -:\n"
training_images, testing_images, training_labels, testing_labels = cross_validation.train_test_split(images,labels, test_size=0.2, random_state=0)
clf = clf.fit(training_images,training_labels)
score = clf.score(testing_images,testing_labels)
predicted = clf.predict(testing_images)
print prec(score)
outfile.write(str(clf))
outfile.write(confusion_matrix(testing_labels, predicted))
print confusion_matrix(testing_labels, predicted)
# outfile.write(metrics.classification_report(testing_labels, predicted))
Output in file-
302e 3939 3338 374c 696e 6561 7253 5643
2843 3d31 2e30 2c20 636c 6173 735f 7765
...
Use the following to print the matrix to file properly:
with open(filename, 'w') as f:
f.write(np.array2string(confusion_matrix(y_test, pred), separator=', '))
Because outfile.write(confusion_matrix(testing_labels, predicted)) will write the matrix out in binary format. If you want write it in human readable text, try this if you are using python 2.x
print >> outfile, confusion_matrix(testing_labels, predicted)
It just redirect the stdout to outfile

Bokeh's hovertool inipython refuses to display tooltips

I literally copy and pasted the example of how to use the hover tool from bokeh's documentation and I still can't get this damn thing to work. I just want bokeh's hover tool to display the x and y coordinates. I think I've implemented it correctly but let me know if anything's wrong. (The ASCII file reads in flawlessly and the graph plots correctly and all the other tools work)
from bokeh.plotting import *
from bokeh.objects import HoverTool
from collections import OrderedDict
output_notebook()
%matplotlib inline
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import mpld3
from mpld3 import plugins, utils
mpld3.enable_notebook()
from pylab import *
import pandas as pd
chip1 = pd.io.parsers.read_table("Chip1_Buffer_ASCII", sep=";")
chip2 = pd.io.parsers.read_table("Chip2_Buffer_ASCII", sep=";")
chip3 = pd.io.parsers.read_table("Chip3_Buffer_ASCII", sep=";")
chip1_1=chip1
chip1_2=chip2
chip1_3=chip3
chip1_1["Frequency (Hz)"]=chip1["Frequency (Hz)"].map(lambda x: math.log10(x))
chip1_2["Frequency (Hz)"]=chip2["Frequency (Hz)"].map(lambda x: math.log10(x))
chip1_3["Frequency (Hz)"]=chip3["Frequency (Hz)"].map(lambda x: math.log10(x))
diff_1_2 = chip1 - chip2
diff_1_2["Frequency (Hz)"] = chip1_1["Frequency (Hz)"]
source1 = ColumnDataSource(chip1_1.to_dict("list"))
source2 = ColumnDataSource(chip1_2.to_dict("list"))
source3 = ColumnDataSource(chip1_3.to_dict("list"))
source4=ColumnDataSource(diff_1_2.to_dict("list"))
import bokeh.plotting as bk
bk.figure(plot_width=600, # in units of px
plot_height=600,
title="Hello World!",
tools="pan,wheel_zoom,box_zoom,select,reset,hover")
bk.hold()
bk.line("Frequency (Hz)", "-Phase (°)",line_width=2,source=source1,logx=True,color="red",xlim=[0, 10000])
bk.line("Frequency (Hz)", "-Phase (°)",line_width=2,source=source2,logx=True,color="green",xlim=[0, 10000])
bk.line("Frequency (Hz)", "-Phase (°)",line_width=2,source=source3,logx=True,color="orange",xlim=[0, 10000])
bk.line("Frequency (Hz)", "-Phase (°)",line_width=2,source=source4,logx=True,color="orange",xlim=[0, 10000])
hover = bk.curplot().select(dict(type=HoverTool))
hover.tooltips=OrderedDict([
("(x,y)", "($x, $y)"),
("index", "$index")
])
bk.show()