ValueError: Failed to convert a NumPy array to a Tensor - keras

I used `
x_train = np.array([np.array(val) for val in x_train])
y_train = np.array([np.array(val) for val in y_train])
`
but I failed to convert numpy to tensor
My code is `
x_train = np.array([np.array(val) for val in x_train])
y_train = np.array([np.array(val) for val in y_train])
model.fit(x_train,y_train,epochs =5,batch_size = 128,validation_split = 0.2,shuffle =True)
test_loss,test_acc = model.evaluate(x_test,y_test)
print('Test loss',test_loss)
print('Accuracy',test_acc)
`
Error:
ValueError Traceback (most recent call last)
<ipython-input-39-43fd775bb14b> in <module>
1 x_train = np.array([np.array(val) for val in x_train])
2 y_train = np.array([np.array(val) for val in y_train])
----> 3 model.fit(x_train,y_train,epochs =5,batch_size = 128,validation_split = 0.2,shuffle =True)
4 test_loss,test_acc = model.evaluate(x_test,y_test)
5 print('Test loss',test_loss)
1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
100 dtype = dtypes.as_dtype(dtype).as_datatype_enum
101 ctx.ensure_initialized()
--> 102 return ops.EagerTensor(value, ctx.device_name, dtype)
103
104
ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray).
My model:
`
model = tf.keras.Sequential([
tf.keras.layers.Embedding(words,embed_size,input_shape =(x_train.shape[0],)),
tf.keras.layers.Conv1D(128,3,activation = 'relu'),
tf.keras.layers.MaxPooling1D(),
tf.keras.layers.LSTM(128,activation = 'tanh'),
tf.keras.layers.Dense(10,activation='relu',input_dim=300),
tf.keras.layers.Dense(1,activation='sigmoid',input_dim=300) ])
model.summary()
`

The error you are getting is because of the data type of an array, as Tensorflow models do not support Object data type, So, try to cast these tensors. I am casting it to float32.
x_train = np.array([np.array(val) for val in x_train])
y_train = np.array([np.array(val) for val in y_train])
x_train = tf.cast(x_train , dtype=tf.float32)
y_train = tf.cast(y_train , dtype=tf.float32)

Related

ValueError: shapes (1,) and (784,100) not aligned: 1 (dim 0) != 784 (dim 0)

I've been trying at this problem for a while I want to use MinMaxScale but it's in 3d array, when I convert to 2d it gives me that it's un aligned and i don't know what to do now to align the data
mport numpy as np
from keras.datasetes import mnist
from keras.utils import np_utils
from sklearn import preprocessing
# load MNIST from server
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# reshape
x_train = x_train.reshape(x_train.shape[0], 1, 28*28)
x_train = x_train.astype('float32')
x_train=x_train.reshape(-1,1)
y_train=y_train.reshape(-1,1)
#MinMax scaler
min_max_scaler = preprocessing.MinMaxScaler()
x_train = min_max_scaler.fit_transform(x_train)
# encode output which is a number in range [0,9] into a vector of size 10
y_train = np_utils.to_categorical(y_train)
#reshape
x_test = x_test.reshape(x_test.shape[0], 1, 28*28)
x_test = x_test.astype('float32')
x_train = min_max_scaler.fit_transform(x_train)
#convert vector to binary
y_test = np_utils.to_categorical(y_test)
# Network
net = Network()
net.add(FCLayer(28*28, 100)) # input_shape=(1, 28*28) ; output_shape=(1, 100)
net.add(ActivationLayer(tanh, tanh_prime))
net.add(FCLayer(100, 50)) # input_shape=(1, 100) ; output_shape=(1, 50)
net.add(ActivationLayer(tanh, tanh_prime))
net.add(FCLayer(50, 10)) # input_shape=(1, 50) ; output_shape=(1, 10)
net.add(ActivationLayer(tanh, tanh_prime))
#Fit and loss
net.use(mse, mse_prime)
net.fit(x_train[:10000], y_train[:10000], epochs=50, learning_rate=0.1)
out = net.predict(x_test)
evaluate()
I got the following error:
42 #Fit and loss
43 net.use(mse, mse_prime)
---> 44 net.fit(x_train[:10000], y_train[:10000], epochs=50, learning_rate=0.1)
46 out = net.predict(x_test)
47 evaluate()
Network.fit(self, x_train, y_train, epochs, learning_rate)
50 output = x_train[j]
51 for layer in self.layers:
---> 52 output = layer.forward_propagation(output)
54 # compute loss (for display purpose only)
55 err += self.loss(y_train[j], output)
FCLayer.forward_propagation(self, input_data)
13 def forward_propagation(self, input_data):
14 self.input = input_data
---> 15 self.output = np.dot(self.input, self.weights) + self.bias
16 return self.output
File <__array_function__ internals>:180, in dot(*args, **kwargs)
ValueError: shapes (1,) and (784,100) not aligned: 1 (dim 0) != 784 (dim 0)
do you know how I can change the shape so it's aligned, and why does this error happen in the fist place I've searched a lot but I didn't find anything useful

How to convert ndarray to spark dataframe for mlflow prediction?

I have a trained model, and test input from mnist
import mlflow
from pyspark.sql.functions import struct, col
logged_model = 'runs:/myid/myModel'
loaded_model = mlflow.pyfunc.spark_udf(spark, model_uri=logged_model,
result_type='double')
(x_train, y_train), (x_test, y_test) = mnist.load_data()
shape of x_test is (10000, 28, 28)
However I am having trouble converting ndarray from mnist to spark dataframe so that I can do prediction for my model
mydf = spark.sparkContext.parallelize(x_test).map(lambda x:
[x.tolist()]).toDF(["Doc2Vec"])
output = mydf.withColumn('predictions', loaded_model(struct(*map(col, mydf.columns))))
when I try to output the result
output.show(10)
I got
An exception was thrown from a UDF: 'ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray).'.
What am i missing here?
Here is my training model
import tensorflow as tf
import uuid
BUFFER_SIZE = 10000
BATCH_SIZE = 64
def make_datasets():
(mnist_images, mnist_labels), _ = \
tf.keras.datasets.mnist.load_data(path=str(uuid.uuid4())+'mnist.npz')
dataset = tf.data.Dataset.from_tensor_slices((
tf.cast(mnist_images[..., tf.newaxis] / 255.0, tf.float32),
tf.cast(mnist_labels, tf.int64))
)
dataset = dataset.repeat().shuffle(BUFFER_SIZE).batch(BATCH_SIZE)
return dataset
def build_and_compile_cnn_model():
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(32, 3, activation='relu', input_shape=(28, 28, 1)),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax'),
])
model.compile(
loss=tf.keras.losses.sparse_categorical_crossentropy,
optimizer=tf.keras.optimizers.SGD(learning_rate=0.001),
metrics=['accuracy'],
)
return model
train_datasets = make_datasets()
options = tf.data.Options()
options.experimental_distribute.auto_shard_policy = tf.data.experimental.AutoShardPolicy.DATA
train_datasets = train_datasets.with_options(options)
multi_worker_model = build_and_compile_cnn_model()
multi_worker_model.fit(x=train_datasets, epochs=3, steps_per_epoch=5)

ValueError: could not convert string to float for X_t

I want to use the X_train and Y_train from the Dataframe df.
But I encountered ValueError: could not convert string to float
Thank you in advance.
df = pd.DataFrame()
df['images'] = X_train.tolist()
df['label'] = Y_train.tolist()
df = df.sample(frac=1).reset_index(drop=True)
df.head()
X_train = df['images'].astype('str')
Y_train = df['label'].astype('str')
X_train = np.asarray(X_train).astype(np.float32)
Y_train = np.asarray(Y_train).astype(np.float32)
X_test = np.asarray(X_test).astype(np.float32)
Y_test = np.asarray(Y_test).astype(np.float32)
print (Y_train)

TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType' in implementing ABC

I am implementing artificial bee colony optimization in ANN using [this][1] api. but i am getting this error. This is my code:
def ANN(optimizer = "adam", neurons = 32, batch_size = 32, epochs = 50, activation = "relu", patience =5, loss = 'mse'):
model = Sequential()
model.add(Dense(neurons, input_dim=look_back, activation= activation))
model.add(Dense(neurons, activation= activation))
model.add(Dense(1))
model.compile(optimizer = optimizer, loss = loss)
early_stopping = EarlyStopping(monitor = "loss", patience = patience)
history = model.fit(x_train, y_train, batch_size = batch_size, epochs = epochs, callbacks = [early_stopping], verbose = 0)
return model
boundaries = [(0,2), (0,2), (0,2), (0,2), (10,100), (20,50), (3,20)]
def performance(x_train, y_train, x_test, y_test, optimizer = None, activation = None, loss = None, batch_size = None, neurons = None, epochs = None, patience=None):
model = ANN(optimizer=optimizer, activation= activation, loss=loss, batch_size=batch_size, neurons= neurons, epochs = epochs, patience=patience)
trainScore = model.evaluate(x_train, y_train, verbose=0)
print('Train Score: %.2f MSE (%.2f RMSE)' % (trainScore, math.sqrt(trainScore)))
testScore = model.evaluate(x_test, y_test, verbose=0)
print('Test Score: %.2f MSE (%.2f RMSE)' % (testScore, math.sqrt(testScore)))
trainPredict = model.predict(x_train)
testPredict = model.predict(x_test)
#calculate mean absolute percent error
trainMAPE = mean_absolute_error(y_train, trainPredict)
testMAPE = mean_absolute_error(y_test, testPredict)
return print('testMAPE: %.2f MAPE' % trainMAPE), print('testMAPE: %.2f MAPE' % testMAPE)
writer = pd.ExcelWriter('/content/Scores.xlsx')
for sheetNum in range(1,5):
dataframe = pd.read_excel('Fri.xlsx',sheet_name='Sheet'+str(sheetNum))
# load the dataset
dataset = dataframe.values
dataset = dataset.astype('float32')
train_size = int(len(dataset) * 0.48)
test_size = len(dataset) - train_size
train, test = dataset[0:train_size, :], dataset[train_size:len(dataset), :]
# reshape into X=t and Y=t+1
look_back = 10
x_train, y_train = create_dataset(train, look_back)
x_test, y_test = create_dataset(test, look_back)
# normalize the dataset
scaler = MinMaxScaler(feature_range=(0, 1))
x_train = scaler.fit_transform(x_train)
x_test = scaler.fit_transform(x_test)
abc_obj = abc(performance(x_train, y_train, x_test, y_test), boundaries)
abc_obj.fit()
#Get solution obtained after fit() execution:
solution = abc_obj.get_solution()
This is my error:
TypeError Traceback (most recent call last)
<ipython-input-38-f9098d8d18fc> in <module>()
23 x_train = scaler.fit_transform(x_train)
24 x_test = scaler.fit_transform(x_test)
---> 25 abc_obj = abc(performance(x_train, y_train, x_test, y_test), boundaries)
26 abc_obj.fit()
27
2 frames
/usr/local/lib/python3.7/dist-packages/keras/layers/core.py in __init__(self, units, activation, use_bias, kernel_initializer, bias_initializer, kernel_regularizer, bias_regularizer, activity_regularizer, kernel_constraint, bias_constraint, **kwargs)
1144 activity_regularizer=activity_regularizer, **kwargs)
1145
-> 1146 self.units = int(units) if not isinstance(units, int) else units
1147 self.activation = activations.get(activation)
1148 self.use_bias = use_bias
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
can you help me with this, please? I think i am not defining the function "performance" correctly. but I don't understand how can I make it better.
[1]: https://pypi.org/project/beecolpy/

dimension related problem in training LightGBM for Multiclass Multilable Classification?

I would like to classify by LightGBM algorithm for Multiclass Multilable Classification but I encounter a problem during training because of not being a list the input. DATA
is The length of real rows is 10000
dataset = pd.read_csv('Data.csv')
X = dataset.iloc[:,np.r_[0:6, 7:27]].values
y = dataset.iloc[:,np.r_[6]].values
x_train, x_test, y_train, y_test = train_test_split(X, y,test_size = 0.25, random_state = 0)
from sklearn.preprocessing import StandardScaler
sc=StandardScaler()
x_train = sc.fit_transform(x_train)
x_test = sc.transform(x_test)
import lightgbm as lgb
d_train = lgb.Dataset(x_train, label=y_train)
params = {}
params['learning_rate'] = 0.003
params['boosting_type'] = 'gbdt'
params['objective'] = 'binary'
params['metric'] = 'binary_logloss'
params['sub_feature'] = 0.5
params['num_leaves'] = 10
params['min_data'] = 50
params['max_depth'] = 10
clf = lgb.train(params, d_train, 100)
y_pred=clf.predict(x_test)
for i in range(0,99):
if y_pred[i]>=.5:
y_pred[i]=1
else:
y_pred[i]=0
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
I encounter this problem:
clf = lgb.train(params, d_train, 100)
File "..\lightgbm\engine.py", line 228, in train
...
File "..\lightgbm\basic.py", line 1336, in set_label
label = list_to_1d_numpy(_label_from_pandas(label), name='label')
File "..\lightgbm\basic.py", line 86, in list_to_1d_numpy
"It should be list, numpy 1-D array or pandas Series".format(type(data).__name__, name))
This error is found in basic.py in a function: """Convert data to numpy 1-D array.""" While when I have changed my data to 1D by
y_train = np.reshape(y_train, [1,trainsize])
x_train = np.reshape(x_train, [1,trainsize*26])
The problem is not solved!
Then I use ravel to make 1D for x_train, y_train
x_train = np.ravel(x_train)
y_train = np.ravel(y_train)
but new error is shown:
\lib\site-packages\lightgbm\basic.py", line 872, in __init_from_np2d
raise ValueError('Input numpy.ndarray must be 2 dimensional')
ValueError: Input numpy.ndarray must be 2 dimensional
What is wrong? How I can solve this?

Resources