Tensorflow function RAM usage keeps rising - python-3.x

I have a very simple tensorflow-based function that takes a tensor of shape (1, 6, 64, 64, 64, 1) and returns a tensor of shape (1, 6, 3) containing the centre of mass of each (64, 64, 64) volume in the original tensor. I works without any problems, but every time my loop (see below) goes into the next iteration, the RAM used in my pc increases. This limits me to about 500 samples before I ran completely out. I assume I'm missing something somewhere but I'm not experienced enough to know where.
code:
import tensorflow as tf
import pickle
import scipy.io
import scipy.ndimage
import sys
from os import listdir
from os.path import isfile, join
import numpy as np
def get_raw_centroids(lm_vol):
# Find centres of mass for each landmark
lm_vol *= tf.cast(tf.greater(lm_vol, 0.75), tf.float64)
batch_size, lm_size, vol_size = lm_vol.shape[:3]
xx, yy, zz = tf.meshgrid(tf.range(vol_size), tf.range(
vol_size), tf.range(vol_size), indexing='ij')
coords = tf.stack([tf.reshape(xx, (-1,)), tf.reshape(yy, (-1,)),
tf.reshape(zz, (-1,))], axis=-1)
coords = tf.cast(coords, tf.float64)
volumes_flat = tf.reshape(lm_vol, [-1, int(lm_size), int(vol_size * vol_size * vol_size), 1])
total_mass = tf.reduce_sum(volumes_flat, axis=2)
raw_centroids = tf.reduce_sum(volumes_flat * coords, axis=2) / total_mass
return raw_centroids
path = '/home/mosahle/Avg_vol_tf/'
lm_data_path = path + 'MAT_data_volumes/'
files = [f for f in listdir(lm_data_path) if isfile(join(lm_data_path, f))]
files.sort()
for i in range(10):
sess = tf.Session()
print("File {} of {}".format(i, len(files)))
"""
Load file
"""
dir = lm_data_path + files[i]
lm_vol = scipy.io.loadmat(dir)['datavol']
lm_vol = tf.convert_to_tensor(lm_vol, dtype=tf.float64)
lm_vol are the (1, 6, 64, 64, 64, 1) arrays. They're just numpy arrays and are converted into tensors.
"""
Get similarity matrix
"""
pts_raw = get_raw_centroids(lm_vol)
print(sess.run(pts_raw))
sess.close()
I've tried putting the tf.Session() outside the loop as well but it makes no difference.

The issue in the above code is you are creating multiple graphs inside the loop, when you calling the function get_raw_centroids.
Lets consider a simpler example:
def get_raw_centroids(lm_vol):
raw_centroids = lm_vol * 2
return raw_centroids
for i in range(10):
sess = tf.Session()
lm_vol = tf.constant(3)
pts_raw = get_raw_centroids(lm_vol)
print(sess.run(pts_raw))
print('****Graph: ***\n')
print([x for x in tf.get_default_graph().get_operations()])
sess.close()
The output of the above code is:
#6
#****Graph: ***
#[<tf.Operation 'Const' type=Const>,
#<tf.Operation 'mul/y' type=Const>,
#<tf.Operation 'mul' type=Mul>]
#6
#****Graph: ***
#[<tf.Operation 'Const' type=Const>,
# <tf.Operation 'mul/y' type=Const>,
# <tf.Operation 'mul' type=Mul>,
# <tf.Operation 'Const_1' type=Const>,
# <tf.Operation 'mul_1/y' type=Const>,
# <tf.Operation 'mul_1' type=Mul>]
#6
#****Graph: ***
#[<tf.Operation 'Const' type=Const>,
#<tf.Operation 'mul/y' type=Const>,
#<tf.Operation 'mul' type=Mul>,
#<tf.Operation 'Const_1' type=Const>,
#<tf.Operation 'mul_1/y' type=Const>,
#<tf.Operation 'mul_1' type=Mul>,
#<tf.Operation 'Const_2' type=Const>,
#<tf.Operation 'mul_2/y' type=Const>,
#<tf.Operation 'mul_2' type=Mul>]
...
So each loop adds a new graph with new variables along with the old graph.
The correct way to handle the above code is the following:
# Create a placeholder for the input
lm_vol = tf.placeholder(dtype=tf.float32)
pts_raw = get_raw_centroids(lm_vol)
# Session
for i in range(10):
# numpy input
lm_vol_np = 3
# pass the input to the placeholder and get the output of the graph
print(sess.run(pts_raw, {lm_vol: lm_vol_np}))
print('****Graph: ***\n')
print([x for x in tf.get_default_graph().get_operations()])
sess.close()
The output of the code will be:
#6.0
#****Graph: ***
#[<tf.Operation 'Placeholder' type=Placeholder>,
#<tf.Operation 'mul/y' type=Const>,
#<tf.Operation 'mul' type=Mul>]
#6.0
#****Graph: ***
#[<tf.Operation 'Placeholder' type=Placeholder>,
#<tf.Operation 'mul/y' type=Const>,
#<tf.Operation 'mul' type=Mul>]
#6.0
#****Graph: ***
#[<tf.Operation 'Placeholder' type=Placeholder>,
#<tf.Operation 'mul/y' type=Const>,
#<tf.Operation 'mul' type=Mul>]

Related

pytorch lstm output not match hidden state

Here is the code:
import torch
from torch.nn.utils.rnn import pack_sequence, pad_packed_sequence
rnn = torch.nn.LSTM(2, 3, 1, bidirectional=True)
x1= torch.randn(1,2)
x2=torch.randn(2,2)
x=pack_sequence([x1,x2], enforce_sorted=False)
y, (hn, cn) = rnn(x)
y,lens=pad_packed_sequence(y)
print the hidden state result:
print(torch.cat([hn[-2], hn[-1]], dim=1))
get result:
tensor([[-0.0265, -0.1149, -0.0466, 0.1080, 0.0901, 0.0865],
[ 0.0736, -0.2503, -0.0634, 0.0801, 0.1401, -0.0032]],
grad_fn=<CatBackward>)
print the output:
for i,j in enumerate(lens):
print(y[i][j-1])
get result:
tensor([-0.0265, -0.1149, -0.0466, 0.1080, 0.0901, 0.0865],
grad_fn=<SelectBackward>)
tensor([ 0.0736, -0.2503, -0.0634, 0.0932, 0.0962, -0.0915],
grad_fn=<SelectBackward>)
the second tensor is not the same with the hidden state!
Why?

At least one label specified must be in y_true, target vector is numerical

I am implementing an SVM project with this data
here is how I extract the features:
import itertools
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn import svm
from sklearn.metrics import classification_report, confusion_matrix
df = pd.read_csv('loan_train.csv')
df['due_date'] = pd.to_datetime(df['due_date'])
df['effective_date'] = pd.to_datetime(df['effective_date'])
df['dayofweek'] = df['effective_date'].dt.dayofweek
df['weekend'] = df['dayofweek'].apply(lambda x: 1 if (x>3) else 0)
Feature = df[['Principal','terms','age','Gender','weekend']]
Feature = pd.concat([Feature,pd.get_dummies(df['education'])], axis=1)
Feature.drop(['Master or Above'], axis = 1,inplace=True)
X = Feature
y = df['loan_status'].replace(to_replace=['PAIDOFF','COLLECTION'], value=[0,1],inplace=False)
creating model and prediction:
clf = svm.SVC(kernel='rbf')
clf.fit(X_train_svm, y_train_svm)
yhat_svm = clf.predict(X_test_svm)
evaluation phase:
def plot_confusion_matrix(cm, classes,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
print(cm)
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
fmt = '.2f' if normalize else 'd'
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, format(cm[i, j], fmt),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.show()
cnf_matrix = confusion_matrix(y_test_svm, yhat_svm, labels=[2,4])
np.set_printoptions(precision=2)
print (classification_report(y_test_svm, yhat_svm))
# Plot non-normalized confusion matrix
plt.figure()
plot_confusion_matrix(cnf_matrix, classes=['Benign(2)','Malignant(4)'],normalize= False, title='Confusion matrix')
here is the error:
Traceback (most recent call last):
File "E:/python/classification_project/classification.py", line 229,in
cnf_matrix = confusion_matrix(y_test_svm, yhat_svm, labels=[2,4])
File "C:\Program Files(x86)\Python38-32\lib\site-packages\sklearn\metrics_classification.py", line 277, in confusion_matrix
raise ValueError("At least one label specified must be in y_true")
ValueError: At least one label specified must be in y_true
I checked this question which was like mine and I changed the y from categorical to numerical but the error is still there!
values in y are 0 and 1 but in confusion_matrix call:
cnf_matrix = confusion_matrix(y_test_svm, yhat_svm, labels=[2,4])
the labels were 2 and 4.
labels in confusion_matrix should be equal to tokens in y vector, ie:
cnf_matrix = confusion_matrix(y_test_svm, yhat_svm, labels=[0,1])
On computing matrix step, Instead using labels=[2,4], I defined labels with the signs labels=['PAIDOFF','COLLECTION']
so here's the computing code :
cnf_matrix = confusion_matrix(y_test, yhat, labels=['PAIDOFF','COLLECTION'])
np.set_printoptions(precision=2)
print (classification_report(y_test, yhat))
# Plot non-normalized confusion matrix
plt.figure()
plot_confusion_matrix(cnf_matrix, classes=['PAIDOFF','COLLECTION'],normalize= False, title='Confusion matrix')

Error when making a decision surface graph on a decision tree

My version of python is 3.
I have adapted this code for my data.
And when trying to make the graph, on the line
X = l_atributos[:, pair]
I have the error:
list indices must be integers or slices, not tuple
But I'm not seeing where the problem is. Could you help me?
for pairidx, pair in enumerate([[0, 1],[0, 2],[0, 3],[1, 2],[1, 3],[2, 3]]):
# We only take the two corresponding features
X = l_atributos[:, pair]
y = etiquetas
# Train
clf = DecisionTreeClassifier().fit(X, y)
# Plot the decision boundary
plt.subplot(2, 3, pairidx + 1)
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, plot_step),
np.arange(y_min, y_max, plot_step))
plt.tight_layout(h_pad=0.5, w_pad=0.5, pad=2.5)
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
cs = plt.contourf(xx, yy, Z, cmap=plt.cm.RdYlBu)
plt.xlabel(['so2', 'no2', 'temp', 'viento', 'precipitacion'][pair[0]])
plt.ylabel(['so2', 'no2', 'temp', 'viento', 'precipitacion'][pair[1]])
# Plot the training points
for i, color in zip(range(n_classes), plot_colors):
idx = np.where(y == i)
plt.scatter(X[idx, 0], X[idx, 1], c=color, label=['nivel 0', 'nivel 1', 'nivel 2', 'nivel 3'][i], cmap=plt.cm.RdYlBu, edgecolor='black', s=15)
plt.suptitle("Decision surface of a decision tree using paired features")
plt.legend(loc='lower right', borderpad=0, handletextpad=0)
plt.axis("tight")
plt.figure()
clf = DecisionTreeClassifier().fit(l_atributos, etiquetas)
plot_tree(clf, filled=True)
plt.show()
The common problem in data structures used to represent the data in the example and your code.
If you print the content of iris example you may see next data:
from sklearn.datasets import load_iris
iris = load_iris()
print(iris.data)
output
array([[5.1, 3.5, 1.4, 0.2],
[4.9, 3. , 1.4, 0.2],
[4.7, 3.2, 1.3, 0.2],
...
As you can see this is the 2D array was wrapped with numpy.array(...) wrapper.
But in your example you have just 2D array:
print(l_atributos[:3])
result
[['66', '26.0', '12.1', '16.0', '0.0'], ['75', '16.0', '10.0', '26.0', '5.9'], ['61', '25.0', '8.0', '23.0', '29.4']]
If you want to use scikit's example with minimum changes just wrap your data with numpy.array:
import numpy as np
l_atributos = np.array([['66', '26.0', '12.1', '16.0', '0.0'], ['75', '16.0', '10.0', '26.0', '5.9'], ['61', '25.0', '8.0', '23.0', '29.4']])

Unable to plot scatter plot because of TypeError

I have a dataset, in which i will be using only a single column to apply kmeans clustering. However while plotting the graph, i am getting "numpy.ndarray". I tried converting to float, but still facing the same issue
Dataframe:
Brim
1234.5
345
675.7
120
110
Code:
from sklearn.cluster import KMeans
import numpy as np
km = KMeans(n_clusters=4, init='k-means++',n_init=10)
km.fit(df1)
x = km.fit_predict(df1)
x
array([0, 0, 0, ..., 3, 3, 3])
np.shape(x)
(1097,)
import matplotlib.pyplot as plt
%matplotlib inline
plt.scatter(df1[x ==1,0], df1[x == 0,1], s=100, c='red')
plt.scatter(df1[x ==1,0], df1[x == 1,1], s=100, c='black')
plt.scatter(df1[x ==2,0], df1[x == 2,1], s=100, c='blue')
plt.scatter(df1[x ==3,0], df1[x == 3,1], s=100, c='cyan')
Error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-62-5f0966ccc828> in <module>()
1 import matplotlib.pyplot as plt
2 get_ipython().run_line_magic('matplotlib', 'inline')
----> 3 plt.scatter(df1[x ==1,0], df1[x == 0,1], s=100, c='red')
4 plt.scatter(df1[x ==1,0], df1[x == 1,1], s=100, c='black')
5 plt.scatter(df1[x ==2,0], df1[x == 2,1], s=100, c='blue')
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
2137 return self._getitem_multilevel(key)
2138 else:
->2139 return self._getitem_column(key)
2140
2141 def _getitem_column(self, key):
~\AppData\Local\Continuum\anaconda3\lib\site-
packages\pandas\core\frame.py in _getitem_column(self, key)
2144 # get column
2145 if self.columns.is_unique:
-> 2146 return self._get_item_cache(key)
2147
2148 # duplicate columns & possible reduce dimensionality
~\AppData\Local\Continuum\anaconda3\lib\site- packages\pandas\core\generic.py in _get_item_cache(self, item)
1838 """Return the cached item, item represents a label indexer."""
1839 cache = self._item_cache
-> 1840 res = cache.get(item)
1841 if res is None:
1842 values = self._data.get(item)
TypeError: unhashable type: 'numpy.ndarray'
If I understood your code correctly, you're trying to slice your DataFrame for plotting, based on the values of x.
For that, you should be using df1.loc[x==1,0] instead of df1[x==1,0] (and so on for all other slices).
In my case, I was trying to pick random 2 features and run KMeans classifier on it.
sample = df[['f1','f2','f3','f4','f5','f6','f7']].sample(2, axis=1)
kmeans_classifier = KMeans(n_clusters=3) # select random features
y_kmeans = kmeans_classifier.fit_predict(sample)
plt.scatter(X[y_kmeans == 0, 0], X[y_kmeans == 0, 1], s = 75, c ='red', label = 'Zero')
Last line was throwing the TypeError. I resolved this by converting the sample DataFrame to Numpy representation with values.
Modified code:
sample = df[['f1','f2','f3','f4','f5','f6','f7']].sample(2, axis=1).values

Multivariate Dirichlet process mixtures for density estimation using pymc3

I want to extend the Austin's example on Dirichlet process mixtures for density estimationto the multivariate case.
The first information about multivariate Gaussian mixture using pymc3 I have found is this issue at Github. People involved in the issue said that there are two different solutions but they don't work for me. For instance, by using the Brandon's Multivariate Extension in a simple model like this:
import numpy as np
import pymc3 as pm
from mvnormal_extension import MvNormal
with pm.Model() as model:
var_x = MvNormal('var_x', mu = 3*np.zeros(2), tau = np.diag(np.ones(2)), shape=2)
trace = pm.sample(100)
I can't obtain the proper Mean around (3,3):
pm.summary(trace)
var_x:
Mean SD MC Error 95% HPD interval
-------------------------------------------------------------------
0.220 1.161 0.116 [-1.897, 2.245]
0.165 1.024 0.102 [-2.626, 1.948]
Posterior quantiles:
2.5 25 50 75 97.5
|--------------|==============|==============|--------------|
-1.897 -0.761 0.486 1.112 2.245
-2.295 -0.426 0.178 0.681 2.634
The other solution can be reproduced thanks to Benavente:
import numpy as np
import pymc3 as pm
import scipy
import theano
from theano import tensor
target_data = np.random.random((500, 16))
N_COMPONENTS = 5
N_SAMPLES, N_DIMS = target_data.shape
# Dirichilet prior.
ALPHA_0 = np.ones(N_COMPONENTS)
# Component means prior.
MU_0 = np.zeros(N_DIMS)
LAMB_0 = 1. * np.eye(N_DIMS)
# Components precision prior.
BETA_0, BETA_1 = 0., 1. # Covariance stds prior uniform limits.
L_0 = 2. # LKJ corr. shape. Larger shape -> more biased to identity.
# In order to convert the upper triangular correlation values to a
# complete correlation matrix, we need to construct an index matrix:
# Source: http://stackoverflow.com/q/29759789/1901296
N_ELEMS = N_DIMS * (N_DIMS - 1) / 2
tri_index = np.zeros([N_DIMS, N_DIMS], dtype=int)
tri_index[np.triu_indices(N_DIMS, k=1)] = np.arange(N_ELEMS)
tri_index[np.triu_indices(N_DIMS, k=1)[::-1]] = np.arange(N_ELEMS)
with pm.Model() as model:
# Component weight prior.
pi = pm.Dirichlet('pi', ALPHA_0, testval=np.ones(N_COMPONENTS) / N_COMPONENTS)
#pi_potential = pm.Potential('pi_potential', tensor.switch(tensor.min(pi) < .01, -np.inf, 0))
###################
# Components plate.
###################
# Component means.
mus = [pm.MvNormal('mu_{}'.format(i), MU_0, LAMB_0, shape=N_DIMS)
for i in range(N_COMPONENTS)]
# Component precisions.
#lamb = diag(sigma) * corr(corr_shape) * diag(sigma)
corr_vecs = [
pm.LKJCorr('corr_vec_{}'.format(i), L_0, N_DIMS)
for i in range(N_COMPONENTS)
]
# Transform the correlation vector representations to matrices.
corrs = [
tensor.fill_diagonal(corr_vecs[i][tri_index], 1.)
for i in range(N_COMPONENTS)
]
# Stds for the correlation matrices.
cov_stds = pm.Uniform('cov_stds', BETA_0, BETA_1, shape=(N_COMPONENTS, N_DIMS))
# Finally re-compose the covariance matrices using diag(sigma) * corr * diag(sigma)
# Source http://austinrochford.com/posts/2015-09-16-mvn-pymc3-lkj.html
lambs = []
for i in range(N_COMPONENTS):
std_diag = tensor.diag(cov_stds[i])
cov = std_diag.dot(corrs[i]).dot(std_diag)
lambs.append(tensor.nlinalg.matrix_inverse(cov))
stacked_mus = tensor.stack(mus)
stacked_lambs = tensor.stack(lambs)
#####################
# Observations plate.
#####################
z = pm.Categorical('z', pi, shape=N_SAMPLES)
#theano.as_op(itypes=[tensor.dmatrix, tensor.lvector, tensor.dmatrix, tensor.dtensor3],
otypes=[tensor.dscalar])
def likelihood_op(values, z_values, mu_values, prec_values):
logp = 0.
for i in range(N_COMPONENTS):
indices = z_values == i
if not indices.any():
continue
logp += scipy.stats.multivariate_normal(
mu_values[i], prec_values[i]).logpdf(values[indices]).sum()
return logp
def likelihood(values):
return likelihood_op(values, z, stacked_mus, stacked_lambs)
y = pm.DensityDist('y', likelihood, observed=target_data)
step1 = pm.Metropolis(vars=mus + lambs + [pi])
step2 = pm.ElemwiseCategoricalStep(vars=[z], values=list(range(N_COMPONENTS)))
trace = pm.sample(100, step=[step1, step2])
I have changed in this code pm.ElemwiseCategoricalStep to pm.ElemwiseCategorical and
logp += scipy.stats.multivariate_normal(mu_values[i], prec_values[i]).logpdf(values[indices])
by
logp += scipy.stats.multivariate_normal(mu_values[i], prec_values[i]).logpdf(values[indices]).sum()
but I get this exception:
ValueError: expected an ndarray
Apply node that caused the error: Elemwise{Composite{((i0 + i1) - (i2 + i3))}}[(0, 0)](Sum{acc_dtype=float64}.0, FromFunctionOp{likelihood_op}.0, Sum{acc_dtype=float64}.0, FromFunctionOp{likelihood_op}.0)
Toposort index: 101
Inputs types: [TensorType(float64, scalar), TensorType(float64, scalar), TensorType(float64, scalar), TensorType(float64, scalar)]
Inputs shapes: [(), (), (), ()]
Inputs strides: [(), (), (), ()]
Inputs values: [array(-127.70516572917249), -13460.012199423296, array(-110.90354888959129), -13234.61313535326]
Outputs clients: [['output']]
HINT: Re-running with most Theano optimization disabled could give you a back-trace of when this node was created. This can be done with by setting the Theano flag 'optimizer=fast_compile'. If that does not work, Theano optimizations can be disabled with 'optimizer=None'.
HINT: Use the Theano flag 'exception_verbosity=high' for a debugprint and storage map footprint of this apply node.
I appreciate any help.
Thanks!

Resources