how to cast an array to a matrix? - python-3.x

I have a dataset with the following shapes: (2400, 2) (2400,) (1600, 2) (1600,)
My task is to perform non-linear separable classification by binary logistic regression.
But I get the following error in visualization part:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-19-2754b9327868> in <module>()
4
5 # Plot different regions and color them
----> 6 output = output.reshape(x_vals.shape)
7 plt.imshow(output, interpolation='nearest',
8 extent=(x_min, x_max, y_min, y_max),
ValueError: cannot reshape array of size 2880000 into shape (1200,1200)
How can I reshape array into matrix?
Below is my implementation for the reference:
num_features = 2
learning_rate = 0.0001
training_steps = 4000
batch_size = 32
display_step = 50
x_train, y_train = map(list, zip(*[(x,y) for x,y in zip(x_train, y_train) if y==0 or y==1]))
x_test, y_test = map(list, zip(*[(x,y) for x,y in zip(x_test, y_test) if y==0 or y==1]))
x_train, x_test = np.array(x_train, np.float32), np.array(x_test, np.float32)
y_train, y_test = np.array(y_train, np.int64), np.array(y_test, np.int64)
x_train, x_test = x_train.reshape([-1, num_features]), x_test.reshape([-1, num_features])
x_train, x_test = x_train/255., x_test/255.
train_data = tf.data.Dataset.from_tensor_slices((x_train, y_train))
train_data = train_data.repeat().shuffle(5000).batch(batch_size).prefetch(1)
b = tf.Variable(tf.ones((num_features, 2)) * 0.000001, name = "weight")
b0 = tf.Variable(0., name = "bias")
def logistic_regression(x, b, b0):
return 1. / (1. + tf.exp(-tf.matmul(x, b) - b0))
def loglikelihood(p, y_true):
return tf.reduce_sum(tf.one_hot(y_true, 2) * tf.math.log(p), axis=-1)
def accuracy(y_pred, y_true):
correct_prediction = tf.equal(tf.argmax(y_pred, axis=-1), y_true)
return tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
optimizer = tf.optimizers.Adam()
for step, (batch_x, batch_y) in enumerate(train_data.take(training_steps), 1):
with tf.GradientTape() as g:
g.watch([b, b0])
p = logistic_regression(batch_x, b, b0)
ll = loglikelihood(p, batch_y)
ll_sum = tf.reduce_mean(ll)
grad_b, grad_b0 = g.gradient(ll_sum, [b, b0])
optimizer.apply_gradients(zip([grad_b, grad_b0], [b, b0]))
if step % display_step == 0:
p = logistic_regression(batch_x, b, b0)
acc = accuracy(p, batch_y)
p = logistic_regression(x_test, b, b0)
val_acc = accuracy(p, y_test)
print("step: %i, acc: %f, val_acc %f" % (step, acc, val_acc))
def predict(x_test):
return tf.round(logistic_regression(x_test, b, b0))
import numpy as np
x_min, y_min = -12, -12
x_max, y_max = 12, 12
x_vals, y_vals = np.meshgrid(np.arange(x_min, x_max, 0.02), np.arange(y_min, y_max, 0.02))
xy_grid = pd.DataFrame(zip(x_vals.ravel(), y_vals.ravel()), dtype=np.float32)
# Predict output labels for all the points on the grid
output = predict(xy_grid.to_numpy()).numpy()
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 1)
# Plot different regions and color them
output = output.reshape(x_vals.shape)
plt.imshow(output, interpolation='nearest',
extent=(x_min, x_max, y_min, y_max),
cmap=plt.cm.Paired,
aspect='auto',
origin='lower')
pd.DataFrame(np.concatenate([x_train,
np.expand_dims(y_train, axis=-1)], axis=1)).plot.scatter(0, 1, c=2, colormap='viridis', ax=ax)
The expected result should be like this:
expected image
But I get the following image:
resulting image

The error is caused by x_vals.shape being too small. Hence numpy cannot reshape the data as you would be losing some data.
You need to change the shape of x_vals to be whatever shape output you expect you see.
For example, if you want a 1200x2400 image you can do:
x_min, y_min = -12, -12
x_max, y_max = 12, 12
x_vals, y_vals = np.meshgrid(np.arange(x_min, x_max, 0.01), np.arange(y_min, y_max, 0.02))
print(x_vals.shape) # shows x_vals.shape = (1200, 2400) which is 28880000 elements
output = np.arange(2880000)
output = output.reshape(x_vals.shape) # this will now work

Related

InvalidArgumentError (see above for traceback):logits and labels must be broadcastable: logits_size=[183,2] labels_size=[20,2]

This is a learning-based rna and disease prediction code using cnn that I downloaded from github. The output is accuracy and auc values, but the result is very unstable (occasionally 0.3, occasionally 0.8).
I don't know what the reason is, but the division of training set and verification set in this article is a self-defined function, so I want to try 10 cross-verification. However, when I write the cross-validation code, the problem as shown in the title appears.
This is the code that divides the training set and the verification set in the source code.
def get_data(args):
input_data, input_label = dh.get_samples(args)
input_data = standard_scale(input_data)
dev_sample_percentage = args.dev_percentage
test_sample_percentage = args.test_percentage
x = np.array(input_data)
Randomly shuffle data
np.random.seed(10)
shuffle_indices = np.random.permutation(np.arange(len(input_label)))
input_data = [x[i] for i in shuffle_indices]
input_label = [input_label[i] for i in shuffle_indices]
dev_sample_index = -2 * int(dev_sample_percentage * float(len(input_label)))
test_sample_index = -1 * int(test_sample_percentage * float(len(input_label)))
x_train, x_dev, test_data = input_data[:dev_sample_index], input_data[dev_sample_index:test_sample_index], input_data[test_sample_index:]
y_train, y_dev, test_label = input_label[:dev_sample_index], input_label[dev_sample_index:test_sample_index], input_label[test_sample_index:]
return x_train, x_dev, test_data, y_train, y_dev, test_label
This is my modified code.
def get_data(args):
input_data, input_label = dh.get_samples(args)
input_data = standard_scale(input_data)
dev_sample_percentage = args.dev_percentage
test_sample_percentage = args.test_percentage
x = np.array(input_data)
y = np.array(input_label)
kf = KFold(n_splits=10)
d = kf.split(x)
for train_idx, test_idx in d:
x_train = x[train_idx]
x_dev = x[test_idx]
l=kf.split(y)
for train_idx ,test_idx in l:
y_train=y[train_idx]
y_dev=y[test_idx]
test_sample_index = -1 * int(test_sample_percentage * float(len(input_label)))
test_data=input_data[test_sample_index:]
test_lable=input_label[test_sample_index:]
return x_train,x_dev,y_train, y_dev,test_data,test_lable
This is a screenshot of the error.
This is the complete code of this part.
#! /usr/bin/env python
import tensorflow as tf
import numpy as np
import os
import argparse
import data_helpers as dh
from sklearn.metrics import roc_curve, auc
from sklearn.metrics import precision_recall_curve
from sklearn import metrics
from tensorflow.contrib import learn
import matplotlib.pyplot as plt
import sklearn.preprocessing as prep
from sklearn.metrics import average_precision_score
from sklearn.model_selection import KFold
def parse_args():
parser = argparse.ArgumentParser(description="Run CNN.")
## the input file
##disease-gene relationships and miRNA-gene relatiohships
parser.add_argument('--input_disease_miRNA', nargs='?', default='..\..\data\CNN\disease-miro-1024-sigmoid.csv',
help='Input disease_gene_relationship file')
parser.add_argument('--input_label',nargs = '?',default='..\..\data\CNN\label.csv',
help='sample label')
parser.add_argument('--batch_size', nargs='?', default=64,
help = 'number of samples in one batch')
parser.add_argument('--training_epochs', nargs='?', default=1,
help= 'number of epochs in SGD')
parser.add_argument('--display_step', nargs='?', default=10)
parser.add_argument('--test_percentage', nargs='?', default=0.1,
help='percentage of test samples')
parser.add_argument('--dev_percentage', nargs='?', default=0.1,
help='percentage of validation samples')
parser.add_argument('--L2_norm', nargs='?', default=0.001,
help='percentage of validation samples')
parser.add_argument('--keep_prob', nargs='?', default=0.5,
help='keep_prob when using dropout option')
parser.add_argument('--optimizer', nargs='?', default=tf.train.AdamOptimizer,
help='optimizer for learning weights')
parser.add_argument('--learning_rate', nargs='?', default=1e-3,
help='learning rate for the SGD')
return parser.parse_args()
def standard_scale(X_train):
preprocessor = prep.StandardScaler().fit(X_train)
X_train = preprocessor.transform(X_train)
return X_train
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev= 0.1)
weights = tf.Variable(initial)
return weights
def bias_variable(shape):
initial = tf.constant(0.1, shape = shape)
return tf.Variable(initial)
def conv2d(x,W):
return tf.nn.conv2d(x,W,strides=[1,1,1,1], padding= "VALID")
def max_pool_2(x, W):
return tf.nn.max_pool(x, ksize = W, strides= [1,10,1,1], padding= "VALID")
def get_data(args):
input_data, input_label = dh.get_samples(args)
input_data = standard_scale(input_data)
dev_sample_percentage = args.dev_percentage
test_sample_percentage = args.test_percentage
x = np.array(input_data)
y = np.array(input_label)
kf = KFold(n_splits=10)
d = kf.split(x)
for train_idx, test_idx in d:
x_train = x[train_idx]
x_dev = x[test_idx]
l=kf.split(y)
for train_idx ,test_idx in l:
y_train=y[train_idx]
y_dev=y[test_idx]
test_sample_index = -1 * int(test_sample_percentage * float(len(input_label)))
test_data=input_data[test_sample_index:]
test_lable=input_label[test_sample_index:]
return x_train,x_dev,y_train, y_dev,test_data,test_lable
# # Randomly shuffle data
# np.random.seed(10)
# shuffle_indices = np.random.permutation(np.arange(len(input_label)))
# input_data = [x[i] for i in shuffle_indices]
# input_label = [input_label[i] for i in shuffle_indices]
# dev_sample_index = -2 * int(dev_sample_percentage * float(len(input_label)))
# test_sample_index = -1 * int(test_sample_percentage * float(len(input_label)))
# x_train, x_dev, test_data = input_data[:dev_sample_index], input_data[dev_sample_index:test_sample_index], input_data[test_sample_index:]
# y_train, y_dev, test_label = input_label[:dev_sample_index], input_label[dev_sample_index:test_sample_index], input_label[test_sample_index:]
#
# return x_train, x_dev, test_data, y_train, y_dev, test_label
def deepnn(x, keep_prob, args):
with tf.name_scope('reshape'):
x = tf.reshape(x, [-1, 1024, 1, 1])
with tf.name_scope('conv_pool'):
filter_shape = [4, 1, 1, 4]
W_conv = weight_variable(filter_shape)
b_conv = bias_variable([4])
h_conv = tf.nn.relu(conv2d(x, W_conv) + b_conv)
h_pool = tf.nn.max_pool(h_conv, ksize = [1, 4, 1, 1], strides= [1,4,1,1], padding= "VALID")
# filter_shape2 = [4,1,4,4]
# W_conv2 = weight_variable(filter_shape2)
# b_conv2 = bias_variable([4])
# h_conv2 = tf.nn.relu(conv2d(h_pool, W_conv2) + b_conv2)
# h_pool2 = tf.nn.max_pool(h_conv2, ksize=[1,4,1,1], strides= [1,4,1,1],padding="VALID")
regula = tf.contrib.layers.l2_regularizer(args.L2_norm)
h_input1 = tf.reshape(h_pool,[-1, 255 * 4])
W_fc1 = weight_variable([255* 4, 50])
b_fc1 = bias_variable([50])
h_input2 = tf.nn.relu(tf.matmul(h_input1, W_fc1) + b_fc1)
h_keep = tf.nn.dropout(h_input2, keep_prob)
W_fc2 = weight_variable([50, 2])
b_fc2 = bias_variable([2])
h_output = tf.matmul(h_keep, W_fc2) + b_fc2
regularizer = regula(W_fc1) + regula(W_fc2)
return h_output, regularizer
def main(args):
with tf.device('/cpu:0'):
x_train, x_dev, test_data, y_train, y_dev, test_label = get_data(args)
input_data = tf.placeholder(tf.float32, [None, 1024])
input_label = tf.placeholder(tf.float32, [None, 2])
keep_prob = tf.placeholder(tf.float32)
y_conv, losses = deepnn(input_data, keep_prob, args)
y_res = tf.nn.softmax(y_conv)
with tf.name_scope('loss'):
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=y_conv, labels=input_label)
cross_entropy = tf.reduce_mean(cross_entropy)
los = cross_entropy + losses
with tf.name_scope('optimizer'):
optimizer = args.optimizer
learning_rate = args.learning_rate
train_step = optimizer(learning_rate).minimize(los)
# optimizer = tf.train.MomentumOptimizer(learning_rate= 0.02, momentum=)
# train_step = optimizer.minimize(cross_entropy)
with tf.name_scope('accuracy'):
predictions = tf.argmax(y_conv, 1)
correct_predictions = tf.equal(predictions, tf.argmax(input_label, 1))
correct_predictions = tf.cast(correct_predictions, tf.float32)
accuracy = tf.reduce_mean(correct_predictions)
batch_size = args.batch_size
num_epochs = args.training_epochs
display_step = args.display_step
k_p = args.keep_prob
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
batches = dh.batch_iter(list(zip(x_train, y_train)), batch_size, num_epochs)
for i, batch in enumerate(batches):
x_batch, y_batch = zip(*batch)# 按batch把数据拿进来
train_step.run(feed_dict={input_data: x_batch, input_label: y_batch, keep_prob: k_p})
if i % display_step == 0:
loss = sess.run(los, feed_dict={input_data: x_train, input_label: y_train, keep_prob: 1.0})
#print('after training loss = %f' % loss)
y_predict = sess.run(y_res, feed_dict={input_data: x_dev, input_label: y_dev, keep_prob: 1.0})[:, 1]
loss = sess.run(los, feed_dict={input_data: x_dev, input_label: y_dev, keep_prob: 1.0})
#print('test loss = %f' % loss)
false_positive_rate1, true_positive_rate1, thresholds1 = roc_curve(np.array(y_dev)[:, 1], y_predict)
roc_auc1 = auc(false_positive_rate1, true_positive_rate1)
# print(roc_auc1)
###print(accuracy.eval(feed_dict={input_data: x_train, input_label:y_train, keep_prob: 1.0}))
print('accuracy=',accuracy.eval(feed_dict={input_data: test_data, input_label: test_label, keep_prob: 1.0}))
y_predict = sess.run(y_res, feed_dict={input_data: test_data, input_label: test_label, keep_prob: 1.0})[:, 1]
false_positive_rate1, true_positive_rate1, thresholds1 = roc_curve(np.array(test_label)[:, 1], y_predict)
roc_auc1 = auc(false_positive_rate1, true_positive_rate1)
print('roc_auc1=',roc_auc1)
# plt.figure()
# lw=2
# plt.title("ROC curve of %s (AUC = %.4f)")
# plt.xlabel("False Positive Rate")
# plt.ylabel("True Positive Rate")
# plt.plot(false_positive_rate1 , true_positive_rate1) # use pylab to plot x and y
# plt.show() # show the plot on the screen
#
# plt.show()
# np.savetxt("result_fp_tp_md_aver.txt", roc_curve(np.array(test_label)[:, 1], y_predict))
# precision, recall ,_ = precision_recall_curve(np.array(test_label)[:, 1], y_predict)
# #
# average_precision = average_precision_score(np.array(test_label)[:, 1], y_predict)
# #
# print('Average precision-recall score: {0:0.2f}'.format(average_precision))
# y_predict[y_predict >= 0.5] = 1
# y_predict[y_predict < 0.5] = 0
# print(y_predict)
# print(metrics.f1_score(np.array(test_label)[:, 1], y_predict))
# np.savetxt("precision_aver.txt", precision)
# np.savetxt("recall_aver.txt", recall)
if __name__ == '__main__':
args = parse_args()
main(args)
please help me!!! thanks a lot!!!

Customized neural network gives 0 accuracy for iris dataset

I've built a neural network for iris classification with the following code:
from sklearn import datasets
from scipy.optimize import minimize
import numpy as np
def train_test_split(X, y):
idx = np.arange(len(X))
train_size = int(len(X) * 0.2)
np.random.shuffle(idx)
X = X[idx]
y = y[idx]
X_train, X_test = X[:train_size], X[train_size:]
y_train, y_test = y[:train_size], y[train_size:]
return X_train, X_test, y_train, y_test
iris = datasets.load_iris()
X = iris.data
y = iris.target
nb_classes = 3
targets = np.array([y]).reshape(-1)
Y = np.eye(nb_classes)[targets]
# randomize = np.arange(len(X))
# np.random.shuffle(randomize)
# X = X[randomize]
# Y = Y[randomize]
X_train, X_test, Y_train, Y_test = train_test_split(X, Y)
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train)
def optimize(X_train, Y_train, X_val=None, Y_val=None, epochs=10, nodes=[], lr=0.15):
hidden_layers = len(nodes) - 1
weights = init_weights(nodes)
for epoch in range(1, epochs+1):
weights = train(X_train, Y_train, lr, weights)
if(epoch % 20 == 0):
print("Epoch {}".format(epoch))
print("Training accuracy:{}".format(acc(X_train, Y_train, weights)))
if X_val.any():
print("Validation Accuracy:{}".format(acc(X_val, Y_val, weights)))
return weights
def init_weights(nodes):
"""Initialize weights with random values in [-1, 1] (including bias)"""
layers, weights = len(nodes), []
for i in range(1, layers):
w = [[np.random.uniform(-1, 1) for k in range(nodes[i-1] + 1)]
for j in range(nodes[i])]
weights.append(np.matrix(w))
return weights
def forward(x, weights, layers):
activations, layer_input = [x], x
for j in range(layers):
activation = sigmoid(np.dot(layer_input, weights[j].T))
activations.append(activation)
layer_input = np.append(1, activation) # Augment with bias
return activations
def back(y, activations, weights, layers):
outputFinal = activations[-1]
error = np.matrix(y - outputFinal) # Error at output
for j in range(layers, 0, -1):
currActivation = activations[j]
if(j > 1):
# Augment previous activation
prevActivation = np.append(1, activations[j-1])
else:
# First hidden layer, prevActivation is input (without bias)
prevActivation = activations[0]
delta = np.multiply(error, sigmoid_gradient(currActivation))
weights[j-1] += lr * np.multiply(delta.T, prevActivation)
w = np.delete(weights[j-1], [0], axis=1) # Remove bias from weights
error = np.dot(delta, w) # Calculate error for current layer
return weights
def train(X, Y, lr, weights):
layers = len(weights)
for i in range(len(X)):
x, y = X[i], Y[i]
x = np.matrix(np.append(1, x)) # Augment feature vector
activations = forward(x, weights, layers)
weights = back(y, activations, weights, layers)
return weights
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def sigmoid_gradient(x):
return np.multiply(x, 1-x)
def predict(item, weights):
layers = len(weights)
item = np.append(1, item) # Augment feature vector
##_Forward Propagation_##
activations = forward(item, weights, layers)
outputFinal = activations[-1].A1
index = FindMaxActivation(outputFinal)
# Initialize prediction vector to zeros
y = [0 for i in range(len(outputFinal))]
y[index] = 1 # Set guessed class to 1
return y # Return prediction vector
def FindMaxActivation(output):
"""Find max activation in output"""
m, index = output[0], 0
for i in range(1, len(output)):
if(output[i] > m):
m, index = output[i], i
return index
def acc(X, Y, weights):
"""Run set through network, find overall accuracy"""
correct = 0
for i in range(len(X)):
# x, y = X[i], list(Y[i])
x, y = X[i], Y[i].tolist()
guess = predict(x, weights)
if(y == guess):
# Guessed correctly
correct += 1
return correct / len(X)
f = len(X[0]) # Number of features
o = len(Y[0]) # Number of outputs / classes
layers = [f, 5, 10, o] # Number of nodes in layers
lr, epochs = 0.15, 100
weights = optimize(X_train, Y_train, X_val, Y_val, epochs=epochs, nodes=layers, lr=lr);
print("Testing Accuracy: {}".format(acc(X_test, Y_test, weights)))
But it gives the result with accuracy of 0s:
Epoch 20
Training accuracy:0.0
Validation Accuracy:0.0
Epoch 40
Training accuracy:0.0
Validation Accuracy:0.0
Epoch 60
Training accuracy:0.0
Validation Accuracy:0.0
Epoch 80
Training accuracy:0.0
Validation Accuracy:0.0
Epoch 100
Training accuracy:0.0
Validation Accuracy:0.0
Testing Accuracy: 0.0
However, if I use dataset with csv format downloaded from here
iris = pd.read_csv("./data/Iris.csv")
iris = iris.sample(frac=1).reset_index(drop=True) # Shuffle
X = iris[['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm']]
X = np.array(X)
from sklearn.preprocessing import OneHotEncoder
one_hot_encoder = OneHotEncoder(sparse=False)
Y = iris.Species
Y = one_hot_encoder.fit_transform(np.array(Y).reshape(-1, 1))
Y[:5]
from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.15)
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size=0.1)
Output:
Epoch 20
Training accuracy:0.9385964912280702
Validation Accuracy:0.9230769230769231
Epoch 40
Training accuracy:0.9912280701754386
Validation Accuracy:0.9230769230769231
Epoch 60
Training accuracy:0.9736842105263158
Validation Accuracy:0.9230769230769231
Epoch 80
Training accuracy:0.9736842105263158
Validation Accuracy:0.9230769230769231
Epoch 100
Training accuracy:0.9824561403508771
Validation Accuracy:0.9230769230769231
Testing Accuracy: 0.9565217391304348
Why this difference?

How to fix Found input variables with inconsistent numbers of samples: [1080, 428] error

I am working on Indian Spontaneous Expression dataset which has 428 images, each of shape (1080, 1920, 3). Classification classes are 4 and its shape is (428, 4). While splitting into training, validation and testing data using train_test_split:
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.1, random_state=42)
I am getting mentioned error.
I tried reshaping the data but anyhow I couldn't succeed.
import cv2 as cv
data=pd.read_excel('/content/drive/My Drive/ISED/details1.xlsx')
count=0
path = data['img_path']
for path in data['img_path']:
count=count+1
temp1 = path.replace("'", "")
imgpath = "/content/drive/My Drive/ISED/" + temp1
imgFile = cv.imread(imgpath)
X = np.asarray(imgFile)
print(X.shape)
print(count)
y = pd.get_dummies(data['emotion']).as_matrix()
# # #storing them using numpy
np.save('fdataXISED', X)
np.save('flabelsISED', y)
# #
print("Preprocessing Done")
print("Number of Features: "+str(len(X[0])))
print("Number of Labels: "+ str(len(y[0])))
print("Number of examples in dataset:"+str(len(X)))
print("X,y stored in fdataXISED.npy and flabelsISED.npy respectively")
num_features = 1920
num_labels = 4
batch_size = 64
epochs = 100
width, height = 1080, 1920
x = np.load('./fdataXISED.npy')
y = np.load('./flabelsISED.npy')
print(x.dtype)
x = x.astype(float)
x -= np.mean(x, axis=0)
x /= np.std(x, axis=0)
print(x.shape," ", y.shape)
#splitting into training, validation and testing data
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.1,
random_state=42)
X_train, X_valid, y_train, y_valid = train_test_split(X_train, y_train,
test_size=0.1, random_state=
I expect proper data split for training.
Problem is here, X = np.asarray(imgFile) in for path in data['img_path']: So, X carries only last image. Please change like this,
X=[]
for path in data['img_path']:
count=count+1
temp1 = path.replace("'", "")
imgpath = "/content/drive/My Drive/ISED/" + temp1
imgFile = cv.imread(imgpath)
imgFile = np.asarray(imgFile)
X.append(imgFile)
X = np.asarray(X)
print(X.shape)
print(count)
And at the end your X will be in shape of (428,1080,1920,3) and y must be in (428,4)
Error occurs because different number of samples in X and y.

How to deal with memory error while using make_meshgrid()

I am trying to visualise SVM classification results using Matplotlib and Scikit-learn, how to handle MemoryError ?!
For my example, I have a small dataset, a table X of 100 examples and 10 features (data table of integer). I did perform classification using SVM of Scikit learn, then I want to visualize the results. But since I have 10 features I can't visualize them directly, so I used PCA after classification to reduce the dimensionality of my data. It did work on IRIS dataset but for my data, it crashes giving me "MemoryError"
#SVM classification
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size = 0.20)
svclassifier = SVC(kernel='linear',gamma='auto',max_iter=1000, decision_function_shape='ovo')
models=svclassifier.fit(X_train, y_train)
y_pred = svclassifier.predict(X_test)
#Plot funtions
def make_meshgrid(x, y, h=.02):
x_min, x_max = x.min() - 1, x.max()+1
y_min, y_max = y.min() - 1, y.max()+1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),np.arange(y_min,
y_max, h))
return xx, yy
def plot_contours(ax, clf, xx, yy, **params):
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
out = ax.contourf(xx, yy, Z, **params)
return out
#PCA for D.R
pca = PCA(n_components=2)
pca.fit(X)
X_pca = pca.transform(X)
print("original shape: ", X.shape)
print("transformed shape:", X_pca.shape)
X=X_pca
#Ploting results
fig, sub = plt.subplots()
plt.subplots_adjust(wspace=0.4, hspace=0.4)
X0, X1 = X[:, 0].flatten(), X[:, 1].flatten()
xx, yy = make_meshgrid(X0, X1)
plot_contours(sub, models, xx, yy, cmap=plt.cm.coolwarm, alpha=0.8)
sub.scatter(X0, X1, c=Y, cmap=plt.cm.coolwarm, s=20, edgecolors='k')
sub.set_xlim(xx.min(), xx.max())
sub.set_ylim(yy.min(), yy.max())
sub.set_xlabel('Sepal length')
sub.set_ylabel('Sepal width')
sub.set_xticks(())
sub.set_yticks(())
sub.set_title("TITLE")
plt.show()
original shape: (100, 10)
transformed shape: (100, 2)
MySQL connection is closed
Traceback (most recent call last):
File "new_data.py", line 123, in <module>
xx, yy = make_meshgrid(X0, X1)
File "new_data.py", line 81, in make_meshgrid
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),np.arange(y_min, y_max, h))
File "/home/.local/lib/python3.5/site-packages/numpy/lib/function_base.py", line 4211, in meshgrid
output = [x.copy() for x in output]
File "/home/.local/lib/python3.5/site-packages/numpy/lib/function_base.py", line 4211, in <listcomp>
output = [x.copy() for x in output]
MemoryError

Tesnsor Flow error :Shape must be rank 2 but is rank 1 for 'MatMul' (op: 'MatMul') with input shapes: [?], [1024]

The problem is i have a single feature input and the shape is one dimensional.I get the data from a stored file which has date and a "Delhi" feature.These feature are meant to be run through the system.Here is the program:
import matplotlib.pyplot as plt
import numpy as np
import json,pickle
from sklearn.preprocessing import MinMaxScaler
import tensorflow as tf
sc = MinMaxScaler()
def initialiseDeep(X_train, X_test, y_train, y_test):
# Model architecture parameters
n_stocks = 1
n_neurons_1 = 1024
n_neurons_2 = 512
n_neurons_3 = 256
n_neurons_4 = 128
n_target = 1
# Placeholder
X = tf.placeholder(dtype=tf.float32,shape=[None])
Y = tf.placeholder(dtype=tf.float32,shape=[None])
# Initializers
sigma = 1
weight_initializer = tf.variance_scaling_initializer(mode="fan_avg", distribution="uniform", scale=sigma)
bias_initializer = tf.zeros_initializer()
# Layer 1: Variables for hidden weights and biases
W_hidden_1 = tf.Variable(weight_initializer([n_stocks, n_neurons_1]))
bias_hidden_1 = tf.Variable(bias_initializer([n_neurons_1]))
# Layer 2: Variables for hidden weights and biases
W_hidden_2 = tf.Variable(weight_initializer([n_neurons_1, n_neurons_2]))
bias_hidden_2 = tf.Variable(bias_initializer([n_neurons_2]))
# Layer 3: Variables for hidden weights and biases
W_hidden_3 = tf.Variable(weight_initializer([n_neurons_2, n_neurons_3]))
bias_hidden_3 = tf.Variable(bias_initializer([n_neurons_3]))
# Layer 4: Variables for hidden weights and biases
W_hidden_4 = tf.Variable(weight_initializer([n_neurons_3, n_neurons_4]))
bias_hidden_4 = tf.Variable(bias_initializer([n_neurons_4]))
# Output layer: Variables for output weights and biases
W_out = tf.Variable(weight_initializer([n_neurons_4, n_target]))
bias_out = tf.Variable(bias_initializer([n_target]))
# Hidden layer
hidden_1 = tf.nn.relu(tf.add(tf.matmul(X, W_hidden_1), bias_hidden_1))
hidden_2 = tf.nn.relu(tf.add(tf.matmul(hidden_1, W_hidden_2), bias_hidden_2))
hidden_3 = tf.nn.relu(tf.add(tf.matmul(hidden_2, W_hidden_3), bias_hidden_3))
hidden_4 = tf.nn.relu(tf.add(tf.matmul(hidden_3, W_hidden_4), bias_hidden_4))
# Output layer (must be transposed)
out = tf.transpose(tf.add(tf.matmul(hidden_4, W_out), bias_out))
# Cost function
mse = tf.reduce_mean(tf.squared_difference(out, Y))
# Optimizer
opt = tf.train.AdamOptimizer().minimize(mse)
# Make Session
net = tf.Session()
# Run initializer
net.run(tf.global_variables_initializer())
# Setup interactive plot
plt.ion()
fig = plt.figure()
ax1 = fig.add_subplot(111)
line1, = ax1.plot(y_test)
line2, = ax1.plot(y_test*0.5)
plt.show()
# Number of epochs and batch size
epochs = 10
batch_size = 15
for e in range(epochs):
# Shuffle training data
shuffle_indices = np.random.permutation(np.arange(len(y_train)))
X_train = X_train[shuffle_indices]
y_train = y_train[shuffle_indices]
# Minibatch training
for i in range(0, len(y_train) // batch_size):
start = i * batch_size
batch_x = X_train[start:start + batch_size]
batch_y = y_train[start:start + batch_size]
# Run optimizer with batch
net.run(opt, feed_dict={X: batch_x, Y: batch_y})
# Show progress
if np.mod(i, 5) == 0:
# Prediction
pred = net.run(out, feed_dict={X: X_test})
line2.set_ydata(pred)
plt.title('Epoch ' + str(e) + ', Batch ' + str(i))
file_name = 'img/epoch_' + str(e) + '_batch_' + str(i) + '.jpg'
plt.savefig(file_name)
plt.pause(0.01)
# Print final MSE after Training
mse_final = net.run(mse, feed_dict={X: X_test, Y: y_test})
print(mse_final)
file_Name = "DataSet"
fileObject = open(file_Name,'rb')
Data=pickle.load(fileObject)
JSON=json.loads(Data)
X=[]
y=[]
DataSet=dict(JSON)
for i in range(1,DataSet['Totaldata']+1):
X.append(int(DataSet[str(i)]['dateFloat']))
y.append(float(DataSet[str(i)]['Delhi']))
test_len=20
X=np.asarray(X)
y=np.asarray(y)
X_train, X_test, y_train, y_test=np.asarray(X[:len(X)-test_len]),np.asarray(X[len(X)-test_len:]),np.asarray(y[:len(X)-test_len]),np.asarray(y[len(X)-test_len:])
initialiseDeep(X_train, X_test, y_train, y_test)
but when i run this i get the following error:
raise ValueError(err.message)
ValueError: Shape must be rank 2 but is rank 1 for 'MatMul' (op: 'MatMul')
with input shapes: [?], [1024].
the cause for the error is this line :
hidden_1 = tf.nn.relu(tf.add(tf.matmul(X, W_hidden_1), bias_hidden_1))
Can someone help me fix this, Iam a newbie on deep learning.
tf.matmul takes inputs of rank >= 2.
So, you can expand the dimensions of the inputs to 2-dimension using tf.expand_dims().
some examples from the documentation.
if 't' is a tensor of shape [2]
1. tf.shape(tf.expand_dims(t, 0)) ## Now the shape is [1, 2]
2. tf.shape(tf.expand_dims(t, 1)) ## Now the shape is [2, 1]
reference: https://www.tensorflow.org/api_docs/python/tf/expand_dims
As cited in the documentation of tensor flow :https://www.tensorflow.org/api_docs/python/tf/matmul
This is a matrix multiplication and it can happen only for matrices with atleast rank 2 and the number of columns in matrix 1 should be equal to number of rows in matrix 2 in tf.matmul(matrix1,matrix2).
Please check the shape's of the two matrices using the function X.shape and W_hidden_1.shape. As I understand from the error this would not be following the above mentioned rule. You can also use tf.transpose to obtain the requisite dimensionality.
Cheers
Check your tensors
Find the tensor with shape = (1222,)
Perform following operation
X=tf.expand_dims(X,1)
It will convert it to shape(1222,1)
And you are good to go

Resources