InvalidArgumentError: Incompatible shapes: [6,2,3] vs. [6,1] what does
this means.
what does incompatible shape means
InvalidArgumentErrorTraceback (most recent call last)
< ipython-input-28-c2078c9c10e8> in ()
----> 1 odel.fit(x, y, epochs=500)
4 frames
/usr/local/lib/python2.7/dist-
packages/tensorflow/python/framework/errors_impl.pyc in __exit__(self,
type_arg, value_arg, traceback_arg)
526 None, None,
527 compat.as_text(c_api.TF_Message(self.status.status)),
--> 528 c_api.TF_GetCode(self.status.status))
529 # Delete the underlying status object from memory otherwise it
stays alive
530 # as there is a reference to status from this from the traceback
due to
InvalidArgumentError: Incompatible shapes: [6,2,3] vs. [6,1]
[ ]
> Blockquote
import tensorflow as tf
import numpy as np
from tensorflow import keras
odel = tf.keras.Sequential([keras.layers.Dense(units=3)])
odel.compile(optimizer='sgd', loss='mean_squared_error')
x = np.array([[[1,2],[6,3]],[[3,4],[6,3]],[[4,5],[6,6]],[[5,6],[6,6]],
[[6,7],[6,4]],[[7,8],[6,2]]], dtype=float)
y = np.array([6, 10, 15, 17, 17,17], dtype=float)
odel.fit(x, y, epochs=500)
a=np.array([[2,3]])`enter code here`
print(odel.predict(a))
There are lots of things that needs to be addressed in the code
1- InputShape
The first layer of tf.Sequential should have an InputShape. It is the shape of one element of the features
2 - the label
The label is at least of two dimensions (likewise for the features). y is of shape [6], whereas it should be of shape [b, 3], 3 being the value of unit of the last layer and b is the number of batches. Looking at the features, b is 6
Related
I am using KerasRegressor from scikeras.wrappers for my regression model.
I have different types of features (onehot encoded and numerical features). Their shapes are :
Shape of A_train: (85, 14)
Shape of B_train: (85, 1)
Shape of C_train: (85, 1)
Shape of D_train: (85, 1)
Shape of E_train: (85, 1)
Shape of F_train: (85, 1)
Shape of G_train: (85, 1)
then, building the model, I inserted the number of their columns as the shape.
def build_model():
#Build the tf.keras function
tf.keras.backend.clear_session()
tf.random.set_seed(42)
tf.keras.models.Sequential()
# Use Input layers, specify input shape
## Categorical input layers
inp_cat_A = tf.keras.layers.Input(shape=(A_train.shape[1],) )
inp_cat_B = tf.keras.layers.Input(shape=(B_train.shape[1],))
inp_cat_C = tf.keras.layers.Input(shape=(C_train.shape[1],))
## Numerical input layer
inp_numD = tf.keras.layers.Input(shape=(D_train.shape[1],))
inp_numE = tf.keras.layers.Input(shape=(E_train.shape[1],))
inp_numF = tf.keras.layers.Input(shape=(F_train.shape[1],))
inp_numG = tf.keras.layers.Input(shape=(G_train.shape[1],))
# Concatenate input layers
ann = tf.keras.layers.Concatenate()([inp_cat_A,
inp_cat_B ,
inp_cat_C ,
inp_numD,
inp_numE,
inp_numF,
inp_numG ])
# Dense layers
ann = tf.keras.layers.Dense(units=32, activation='relu')(ann)
out = tf.keras.layers.Dense(units=1)(ann)
# Define model
model = tf.keras.models.Model(inputs=[inp_cat_A,inp_cat_B ,inp_cat_C, inp_numD,
inp_numE, inp_numF, inp_numG], outputs=out)
optimizer = tf.keras.optimizers.RMSprop()
model.compile(optimizer = optimizer , loss= tf.keras.metrics.mean_squared_error,
metrics=[[tf.keras.metrics.RootMeanSquaredError(name= 'rmse')] ,r2_keras])
#Return the model
return model
Then, inputs were fit and the result rise an error
regressor = KerasRegressor(build_fn=build_model)
np.random.seed(1)
history = regressor.fit( [
A_train.astype('float32'),
B_train.astype('float32'),
C_train.astype('float32'),
D_train.astype('float32') ,
E_train.astype('float32'),
F_train.astype('float32'),
G_train.astype('float32')],
y_train.astype('float32'),
epochs=525,
batch_size=85,
verbose=0
)
The error is:
/usr/local/lib/python3.7/dist-packages/sklearn/utils/validation.py:746:
VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-
or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If
you meant to do this, you must specify 'dtype=object' when creating the ndarray.
array = np.asarray(array, order=order, dtype=dtype)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-37-74d9ffedf0b9> in <module>()
14 epochs=525,
15 batch_size=85,
---> 16 verbose=0
17 )
/usr/local/lib/python3.7/dist-packages/sklearn/utils/validation.py in check_array(array,
accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d,
allow_nd, ensure_min_samples, ensure_min_features, estimator)
744 array = array.astype(dtype, casting="unsafe", copy=False)
745 else:
--> 746 array = np.asarray(array, order=order, dtype=dtype)
747 except ComplexWarning as complex_warning:
748 raise ValueError(
ValueError: could not broadcast input array from shape (85,14) into shape (85,)
When I do the same thing but using the kerasRegression from tensorflow.keras.wrappers.scikit_learn it does not rise any problem.
I am trying to use the KerasRegression from scikeras.wrappers because it is suggested to use this as the other is deprecated, and, of course, now and in the future I would like to use the actualized one.
I tried to reshape the inputs but the problem is not solved. How can I resolve this? Do you have any suggestion?
I want to extract patches from my original images to use them as input for a CNN.
After a little research I found a way to extract patches with
tensorflow.compat.v1.extract_image_patches.
Since these need to be reshaped to "image format" I implemented a method reshape_image_patches to reshape them and store the reshaped patches in an array.
image_patches2 = []
def reshape_image_patches(image_patches, sess, ksize_rows, ksize_cols):
a = sess.run(tf.shape(image_patches))
nr, nc = a[1], a[2]
for i in range(nr):
for j in range(nc):
patch = tf.reshape(image_patches[0,i,j,], [ksize_rows, ksize_cols, 3])
image_patches2.append(patch)
return image_patches2
How can I use this in combination with Keras generators to make these patches the input of my CNN?
Edit 1:
I have tried the approach in Load tensorflow images and create patches
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
dataset = tf.keras.preprocessing.image_dataset_from_directory(
<directory>,
label_mode=None,
seed=1,
subset='training',
validation_split=0.1,
image_size=(900, 900))
get_patches = lambda x: (tf.reshape(
tf.image.extract_patches(
x,
sizes=[1, 16, 16, 1],
strides=[1, 8, 8, 1],
rates=[1, 1, 1, 1],
padding='VALID'), (111*111, 16, 16, 3)))
dataset = dataset.map(get_patches)
fig = plt.figure()
plt.subplots_adjust(wspace=.1, hspace=.2)
images = next(iter(dataset))
for index, image in enumerate(images):
ax = plt.subplot(2, 2, index + 1)
ax.set_xticks([])
ax.set_yticks([])
ax.imshow(image)
plt.show()
In line: images = next(iter(dataset)) I get the error: InvalidArgumentError: Input to reshape is a tensor with 302800896 values, but the requested shape has 9462528
[[{{node Reshape}}]]
Does somebody know how to fix this?
The tf.reshape does not change the order of or the total number of elements in the tensor. The error as states, you are trying to reduce total number of elements from 302800896 to 9462528 . You are using tf.reshape in lambda function.
In below example, I have recreated your scenario where I have the given the shape argument as 2 for tf.reshape which doesn't accommodate all the elements of original tensor, thus throws the error -
Code -
%tensorflow_version 2.x
import tensorflow as tf
t1 = tf.Variable([1,2,2,4,5,6])
t2 = tf.reshape(t1, 2)
Output -
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-3-0ff1d701ff22> in <module>()
3 t1 = tf.Variable([1,2,2,4,5,6])
4
----> 5 t2 = tf.reshape(t1, 2)
3 frames
/usr/local/lib/python3.6/dist-packages/six.py in raise_from(value, from_value)
InvalidArgumentError: Input to reshape is a tensor with 6 values, but the requested shape has 2 [Op:Reshape]
tf.reshape should be in such a way that the arrangement of elements can change but total number of elements must remain the same. So the fix would be to change the shape to [2,3] -
Code -
%tensorflow_version 2.x
import tensorflow as tf
t1 = tf.Variable([1,2,2,4,5,6])
t2 = tf.reshape(t1, [2,3])
print(t2)
Output -
tf.Tensor(
[[1 2 2]
[4 5 6]], shape=(2, 3), dtype=int32)
To solve your problem, either extract patches(tf.image.extract_patches) of size that you are trying to tf.reshape OR change the tf.reshape to size of extract patches.
Will also suggest you to look into other tf.image functionality like tf.image.central_crop and tf.image.crop_and_resize.
I would like to write tensorflow example records to a TFRecordWriter from inside an AutoGraph generated graph.
The documentation for tensorflow 2.0 states the following:
The simplest way to handle non-scalar features is to use tf.serialize_tensor to convert tensors to binary-strings. Strings are scalars in tensorflow.
However, tf.io.serialize_tensor returns a tensor of byte-string. Creating an Example proto requires a bytes list, not a tensor.
How do I write a tf.train.Example to a tf record from inside a graph?
Code to reproduce:
%tensorflow_version 2.x
import tensorflow as tf
#tf.function
def example_write():
writer = tf.io.TFRecordWriter("test.tfr")
x = tf.constant([[0, 1], [2, 3]])
x = tf.io.serialize_tensor(x)
feature = {
"data": tf.train.Features(
bytes_list=tf.train.BytesList(value=[x]))
}
ex = tf.train.Example(features=tf.train.Features(
feature=feature))
writer.write(ex.SerializeToString())
example_write()
and the error
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-df8a97eb17c9> in <module>()
12 writer.write(ex.SerializeToString())
13
---> 14 example_write()
8 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
966 except Exception as e: # pylint:disable=broad-except
967 if hasattr(e, "ag_error_metadata"):
--> 968 raise e.ag_error_metadata.to_exception(e)
969 else:
970 raise
TypeError: in user code:
<ipython-input-6-df8a97eb17c9>:6 example_write *
feature = {
TypeError: <tf.Tensor 'SerializeTensor:0' shape=() dtype=string> has type Tensor, but expected one of: bytes
It's pretty straightforward:
use x = tf.io.serialize_tensor(x).numpy()
I am trying a simple classification report on the output from a Keras model prediction. The format of the inputs are two 1D arrays, but the error is still thrown.
Y_pred = np.squeeze(model.predict(test_data[0:5]))
classification_report(test_labels[0:5], Y_pred)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-235-49afd2f46d17> in <module>()
----> 1 classification_report(test_labels[0:5], Y_pred)
/Library/Python/2.7/site-packages/sklearn/metrics/classification.pyc in classification_report(y_true, y_pred, labels, target_names, sample_weight, digits)
1356
1357 if labels is None:
-> 1358 labels = unique_labels(y_true, y_pred)
1359 else:
1360 labels = np.asarray(labels)
/Library/Python/2.7/site-packages/sklearn/utils/multiclass.pyc in unique_labels(*ys)
97 _unique_labels = _FN_UNIQUE_LABELS.get(label_type, None)
98 if not _unique_labels:
---> 99 raise ValueError("Unknown label type: %s" % repr(ys))
100
101 ys_labels = set(chain.from_iterable(_unique_labels(y) for y in ys))
ValueError: Unknown label type: (array([-0.38947693, 0.18258421, -0.00295772, -0.06293461, -0.29382696]), array([-0.46586546, 0.1359883 , -0.00223112, -0.08303966, -0.29208803]))
Both of the inputs are of the same type, so I am confused why this would not work? I have tried changing the type explicitly to dtype=float and flattening the inputs, but it still does not work.
classification_report works only for classification problems.
If you have a classification problem (eg, binary), use the following
Y_pred = np.squeeze(model.predict(test_data[0:5]))
threshold = 0.5
classification_report(test_labels[0:5], Y_pred > threshold)
threshold will make everything greater than 0.5 (in example above), 1.0
I want to perform GridSearchCV in a RandomForestClassifier, but data is not balanced, so I use StratifiedKFold:
from sklearn.model_selection import StratifiedKFold
from sklearn.grid_search import GridSearchCV
from sklearn.ensemble import RandomForestClassifier
param_grid = {'n_estimators':[10, 30, 100, 300], "max_depth": [3, None],
"max_features": [1, 5, 10], "min_samples_leaf": [1, 10, 25, 50], "criterion": ["gini", "entropy"]}
rfc = RandomForestClassifier()
clf = GridSearchCV(rfc, param_grid=param_grid, cv=StratifiedKFold()).fit(X_train, y_train)
But I get an error:
TypeError Traceback (most recent call last)
<ipython-input-597-b08e92c33165> in <module>()
9 rfc = RandomForestClassifier()
10
---> 11 clf = GridSearchCV(rfc, param_grid=param_grid, cv=StratifiedKFold()).fit(X_train, y_train)
c:\python34\lib\site-packages\sklearn\grid_search.py in fit(self, X, y)
811
812 """
--> 813 return self._fit(X, y, ParameterGrid(self.param_grid))
c:\python34\lib\site-packages\sklearn\grid_search.py in _fit(self, X, y, parameter_iterable)
559 self.fit_params, return_parameters=True,
560 error_score=self.error_score)
--> 561 for parameters in parameter_iterable
562 for train, test in cv)
c:\python34\lib\site-packages\sklearn\externals\joblib\parallel.py in __call__(self, iterable)
756 # was dispatched. In particular this covers the edge
757 # case of Parallel used with an exhausted iterator.
--> 758 while self.dispatch_one_batch(iterator):
759 self._iterating = True
760 else:
c:\python34\lib\site-packages\sklearn\externals\joblib\parallel.py in dispatch_one_batch(self, iterator)
601
602 with self._lock:
--> 603 tasks = BatchedCalls(itertools.islice(iterator, batch_size))
604 if len(tasks) == 0:
605 # No more tasks available in the iterator: tell caller to stop.
c:\python34\lib\site-packages\sklearn\externals\joblib\parallel.py in __init__(self, iterator_slice)
125
126 def __init__(self, iterator_slice):
--> 127 self.items = list(iterator_slice)
128 self._size = len(self.items)
c:\python34\lib\site-packages\sklearn\grid_search.py in <genexpr>(.0)
560 error_score=self.error_score)
561 for parameters in parameter_iterable
--> 562 for train, test in cv)
563
564 # Out is a list of triplet: score, estimator, n_test_samples
TypeError: 'StratifiedKFold' object is not iterable
When I write cv=StratifiedKFold(y_train) I have ValueError: The number of folds must be of Integral type. But when I write `cv=5, it works.
I don't understand what is wrong with StratifiedKFold
I had exactly the same problem. The solution that worked for me is to replace:
from sklearn.grid_search import GridSearchCV
with
from sklearn.model_selection import GridSearchCV
Then it should work fine.
The problem here is an API change as mentioned in other answers, however the answers could be more explicit.
The cv parameter documentation states:
cv : int, cross-validation generator or an iterable, optional
Determines the cross-validation splitting strategy. Possible inputs
for cv are:
None, to use the default 3-fold cross-validation, integer,
to specify the number of folds.
An object to be used as a
cross-validation generator.
An iterable yielding train/test splits.
For integer/None inputs, if y is binary or multiclass, StratifiedKFold
used. If the estimator is a classifier or if y is neither binary nor
multiclass, KFold is used.
So, whatever the cross validation strategy used, all that is needed is to provide the generator using the function split, as suggested:
kfolds = StratifiedKFold(5)
clf = GridSearchCV(estimator, parameters, scoring=qwk, cv=kfolds.split(xtrain,ytrain))
clf.fit(xtrain, ytrain)
It seems that cv=StratifiedKFold()).fit(X_train, y_train) should be changed to cv=StratifiedKFold()).split(X_train, y_train).
The api changed in the latest version. You used to pass y and now you pass just the number when you create the stratifiedKFold object. You pass the y later.