import torch
torch.cuda.is_available()
torch.cuda.current_device()
torch.cuda.get_device_name(0)
torch.cuda.memory_reserved()
torch.cuda.memory_allocated()
torch.cuda.memory_allocated()
var1=torch.FloatTensor([1.0,2.0,3.0]).cuda()
var1
var1.device
import pandas as pd
df=pd.read_csv('diabetes.csv')
df.head()
df.isnull().sum()
import seaborn as sns
import numpy as np
df['Outcome']=np.where(df['Outcome']==1,"Diabetic","No Diabetic")
df.head()
sns.pairplot(df,hue="Outcome")
X=df.drop('Outcome',axis=1).values### independent features
y=df['Outcome'].values###dependent features
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=0)
y_train
import torch
import torch.nn as nn
import torch.nn.functional as F
X_train=torch.FloatTensor(X_train).cuda()
X_test=torch.FloatTensor(X_test).cuda()
y_train=torch.LongTensor(y_train).cuda()
y_test=torch.LongTensor(y_test).cuda()
when I Run this code I got this error:
Traceback (most recent call last):
File "<stdin>", line 24, in <module>
TypeError: expected CPU (got CUDA)
How to can I solve this error?
To transfer the variables to GPU, try the following:
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
X_train=torch.FloatTensor(X_train).to(device)
X_test=torch.FloatTensor(X_test).to(device)
y_train=torch.LongTensor(y_train).to(device)
y_test=torch.LongTensor(y_test).to(device)
Related
I am trying to convert pandas dataframe into Tensorflow dataset to build a model upon. But from_tensor_slices gives error. Any idea to fix it or another way to use pandas df in tensorflow model?
Thanks in advance.
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split
tf.compat.v1.disable_eager_execution()
df = pd.read_csv('insurance.csv')
X = pd.get_dummies(df, columns = ['sex', 'smoker', 'region'])
y = X.pop('charges')
ds = tf.data.Dataset.from_tensor_slices((X.values, y.values))
Error:
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-
packages\tensorflow\python\training\tracking\tracking.py", line 269, in
__del__
File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-
packages\tensorflow\python\framework\ops.py", line 4011, in as_default
AttributeError: 'NoneType' object has no attribute 'get_controller'
I solved it now, and I am posting the answer for those who get the same error.
You need to add the line below before from_tensor_slices() line:
tf.compat.v1.enable_eager_execution()
Need help in figuring this out. I'm not sure what went wrong but the error persists. Looked around but can't find a similar issue.
import matplotlib.pyplot as plt
from PIL import Image
import os
import numpy as np
from skimage import io
from keras.preprocessing.image import ImageDataGenerator
from matplotlib import cm
from mpl_toolkits.axes_grid1 import ImageGrid
import math
%matplotlib inline
import keras
import tensorflow as tf
from keras.models import Model
batch_size=32
datagen_args = dict(rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2,
rescale=1./255)
datagen = ImageDataGenerator(**datagen_args)
train_datagenerator = datagen.flow_from_directory('/content/drive/MyDrive/cats_dogs_small/train',target_size=(128,128),
batch_size=batch_size,interpolation="lanczos",shuffle=True)
valid_datagenerator = datagen.flow_from_directory('/content/drive/MyDrive/cats_dogs_small/validation',target_size=(128,128),
batch_size=batch_size,interpolation="lanczos",shuffle=True)
epochs = 25
hist = Model.fit_generator(train_datagenerator,
steps_per_epoch= math.ceil(train_datagenerator.samples//batch_size),
epochs=epochs, validation_data=valid_datagenerator, validation_steps=math.ceil(valid_datagenerator.samples//batch_size),verbose = 1, workers=8)
The error msg is as such:
TypeError Traceback (most recent call last)
<ipython-input-69-178574fd407f> in <module>()
2 hist = Model.fit_generator(train_datagenerator,
3 steps_per_epoch= math.ceil(train_datagenerator.samples//batch_size),
----> 4 epochs=epochs, validation_data=valid_datagenerator, validation_steps=math.ceil(valid_datagenerator.samples//batch_size),verbose = 1, workers=8)
TypeError: fit_generator() missing 1 required positional argument: 'generator'
fit generator is depreciated, just use model.fit. Note you used Model.fit_generator. You should use model.fit.
I realized I did not define what model is. Added the layers, compiled and tried again it works this time. I'm new in this, still got lots to learn !
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn import linear_model
from sklearn.metrics import confusion_matrix
from sklearn import metrics
from sklearn.preprocessing import LabelEncoder
from google.colab import files
df = files.upload()
df='Dataset.csv'
df=df.dropna()
AttributeError Traceback (most recent call last)
in ()
----> 1 df=df.dropna()
AttributeError: 'str' object has no attribute 'dropna'
You are not loading the file as a dataframe, you just assign the file name of df. Use instead -
df = pd.read_csv('Dataset.csv')
df = df.dropna()
I'm attempting to import max error from sklearn.metrics
from sklearn.metrics import max_error
However when I receive the following message:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-27-861960ac8e03> in <module>
1 from sklearn.metrics import explained_variance_score
----> 2 from sklearn.metrics import max_error
3 from sklearn.metrics import mean_squared_error
4 from sklearn.metrics import r2_score
ImportError: cannot import name 'max_error' from 'sklearn.metrics' (/opt/conda/lib/python3.7/site-packages/sklearn/metrics/__init__.py)
Any advice on how to fix this?
I've been attempting to fit this data by a Linear Regression, following a tutorial on bigdataexaminer. Everything was working fine up until this point. I imported LinearRegression from sklearn, and printed the number of coefficients just fine. This was the code before I attempted to grab the coefficients from the console.
import numpy as np
import pandas as pd
import scipy.stats as stats
import matplotlib.pyplot as plt
import sklearn
from sklearn.datasets import load_boston
from sklearn.linear_model import LinearRegression
boston = load_boston()
bos = pd.DataFrame(boston.data)
bos.columns = boston.feature_names
bos['PRICE'] = boston.target
X = bos.drop('PRICE', axis = 1)
lm = LinearRegression()
After I had all this set up I ran the following command, and it returned the proper output:
In [68]: print('Number of coefficients:', len(lm.coef_)
Number of coefficients: 13
However, now if I ever try to print this same line again, or use 'lm.coef_', it tells me coef_ isn't an attribute of LinearRegression, right after I JUST used it successfully, and I didn't touch any of the code before I tried it again.
In [70]: print('Number of coefficients:', len(lm.coef_))
Traceback (most recent call last):
File "<ipython-input-70-5ad192630df3>", line 1, in <module>
print('Number of coefficients:', len(lm.coef_))
AttributeError: 'LinearRegression' object has no attribute 'coef_'
The coef_ attribute is created when the fit() method is called. Before that, it will be undefined:
>>> import numpy as np
>>> import pandas as pd
>>> from sklearn.datasets import load_boston
>>> from sklearn.linear_model import LinearRegression
>>> boston = load_boston()
>>> lm = LinearRegression()
>>> lm.coef_
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-22-975676802622> in <module>()
7
8 lm = LinearRegression()
----> 9 lm.coef_
AttributeError: 'LinearRegression' object has no attribute 'coef_'
If we call fit(), the coefficients will be defined:
>>> lm.fit(boston.data, boston.target)
>>> lm.coef_
array([ -1.07170557e-01, 4.63952195e-02, 2.08602395e-02,
2.68856140e+00, -1.77957587e+01, 3.80475246e+00,
7.51061703e-04, -1.47575880e+00, 3.05655038e-01,
-1.23293463e-02, -9.53463555e-01, 9.39251272e-03,
-5.25466633e-01])
My guess is that somehow you forgot to call fit() when you ran the problematic line.
I also got the same problem while dealing with linear regression the problem object has no attribute 'coef'.
There are just slight changes in the syntax only.
linreg = LinearRegression()
linreg.fit(X,y) # fit the linesr model to the data
print(linreg.intercept_)
print(linreg.coef_)
I Hope this will help you Thanks