addons
addons copied to clipboard
TypeError: __array__() takes 1 positional argument but 2 were given on using F1Score
Trying to use F1 Score Metric
model.add(keras.layers.Dense(1, activation=activation))
model.compile(loss='mse',
optimizer="rmsprop",
metrics=['accuracy',
F1Score(num_classes=2, average="weighted"),
keras.metrics.Precision(name='precision'),
keras.metrics.Recall(name='recall')])
https://github.com/tensorflow/addons/blob/334cd7ca8fb944aab38164a13d7d2203d7c39605/tensorflow_addons/metrics/f_scores.py#L208
File "/Users/fguimara/.pyenv/versions/3.8.5/envs/pleural_2021/lib/python3.8/site-packages/keras/engine/training_v1.py", line 985, in reset_metrics m.reset_state() File "/Users/fguimara/.pyenv/versions/3.8.5/envs/pleural_2021/lib/python3.8/site-packages/tensorflow_addons/metrics/f_scores.py", line 208, in reset_state K.batch_set_value([(v, reset_value) for v in self.variables]) File "/Users/fguimara/.pyenv/versions/3.8.5/envs/pleural_2021/lib/python3.8/site-packages/tensorflow/python/util/dispatch.py", line 206, in wrapper return target(*args, **kwargs) File "/Users/fguimara/.pyenv/versions/3.8.5/envs/pleural_2021/lib/python3.8/site-packages/keras/backend.py", line 3782, in batch_set_value value = np.asarray(value, dtype=dtype_numpy(x)) File "/Users/fguimara/.pyenv/versions/3.8.5/envs/pleural_2021/lib/python3.8/site-packages/numpy/core/_asarray.py", line 83, in asarray return array(a, dtype, copy=False, order=order) TypeError: array() takes 1 positional argument but 2 were given
absl-py==0.14.1
aiofiles==0.6.0
alembic==1.4.1
anyconfig==0.10.1
anyio==3.3.4
appdirs==1.4.4
appnope==0.1.2
argon2-cffi==21.1.0
arrow==1.2.0
astunparse==1.6.3
async-generator==1.10
attrs==21.2.0
Babel==2.9.1
backcall==0.2.0
binaryornot==0.4.4
black==21.5b1
bleach==4.1.0
Brotli==1.0.9
cachetools==4.2.4
certifi==2021.10.8
cffi==1.15.0
chardet==4.0.0
charset-normalizer==2.0.7
clang==5.0
click==7.1.2
cloudpickle==2.0.0
cookiecutter==1.7.3
coverage==6.0.2
cycler==0.10.0
dash==1.21.0
dash-bootstrap-components==0.13.0
dash-colorscales==0.0.4
dash-core-components==1.17.1
dash-daq==0.5.0
dash-html-components==1.1.4
dash-table==4.12.0
databricks-cli==0.15.0
debugpy==1.5.0
decorator==5.1.0
defusedxml==0.7.1
deprecation==2.1.0
dill==0.3.4
docker==5.0.3
dtale==1.53.0
dynaconf==3.1.5
entrypoints==0.3
et-xmlfile==1.1.0
fastapi==0.66.1
flake8==3.9.2
Flask==2.0.2
Flask-Compress==1.10.1
flask-ngrok==0.0.25
flatbuffers==1.12
fsspec==0.8.7
future==0.18.2
gast==0.4.0
gitdb==4.0.7
GitPython==3.1.24
google-api-core==2.0.1
google-auth==2.3.0
google-auth-oauthlib==0.4.6
google-cloud-bigquery==2.26.0
google-cloud-bigquery-storage==2.8.0
google-cloud-core==2.0.0
google-crc32c==1.2.0
google-pasta==0.2.0
google-resumable-media==2.0.3
googleapis-common-protos==1.53.0
gorilla==0.4.0
greenlet==1.1.2
grpcio==1.41.0
gunicorn==20.1.0
h11==0.12.0
h5py==3.1.0
idna==3.3
importlib-metadata==4.8.1
importlib-resources==5.2.2
iniconfig==1.1.1
intel-tensorflow==0.0.1
ipykernel==6.4.1
ipyparallel==7.1.0
ipython==7.28.0
ipython-genutils==0.2.0
ipywidgets==7.6.5
isort==5.9.3
itsdangerous==2.0.1
jedi==0.18.0
Jinja2==3.0.2
jinja2-time==0.2.0
jmespath==0.10.0
joblib==1.0.1
json5==0.9.6
jsonschema==4.1.0
jupyter==1.0.0
jupyter-client==6.1.12
jupyter-console==6.4.0
jupyter-core==4.8.1
jupyter-server==1.11.1
jupyterlab==3.2.0
jupyterlab-pygments==0.1.2
jupyterlab-server==2.8.2
jupyterlab-widgets==1.0.2
kaleido==0.2.1
kedro==0.17.4
kedro-mlflow==0.7.6
kedro-viz==3.14.0
keras==2.6.0
Keras-Preprocessing==1.1.2
kiwisolver==1.3.1
libcst==0.3.21
llvmlite==0.37.0
lz4==3.1.3
Mako==1.1.5
Markdown==3.3.4
MarkupSafe==2.0.1
matplotlib==3.4.2
matplotlib-inline==0.1.3
mccabe==0.6.1
missingno==0.4.2
mistune==0.8.4
mlflow==1.20.2
mypy-extensions==0.4.3
nbclassic==0.3.2
nbclient==0.5.4
nbconvert==6.2.0
nbformat==5.1.3
nbstripout==0.5.0
nest-asyncio==1.5.1
networkx==2.6.2
notebook==6.4.4
numba==0.54.0
numexpr==2.7.3
numpy==1.19.2
oauthlib==3.1.1
openpyxl==3.0.9
opt-einsum==3.3.0
packaging==21.0
pandas==1.3.3
pandas-gbq==0.15.0
pandocfilters==1.5.0
parso==0.8.2
pathspec==0.9.0
patsy==0.5.1
pexpect==4.8.0
pickleshare==0.7.5
Pillow==8.3.1
pip-tools==5.5.0
plotly==5.1.0
pluggy==0.13.1
poyo==0.5.0
ppscore==1.2.0
prometheus-client==0.11.0
prometheus-flask-exporter==0.18.3
prompt-toolkit==3.0.20
proto-plus==1.19.0
protobuf==3.18.1
psutil==5.8.0
ptyprocess==0.7.0
py==1.10.0
pyarrow==3.0.0
pyasn1==0.4.8
pyasn1-modules==0.2.8
pycodestyle==2.7.0
pycparser==2.20
pydantic==1.8.2
pydata-google-auth==1.2.0
pyflakes==2.3.1
Pygments==2.10.0
pyparsing==2.4.7
pyrsistent==0.18.0
pytest==6.2.5
pytest-cov==2.12.1
pytest-mock==1.13.0
python-dateutil==2.8.2
python-editor==1.0.4
python-json-logger==2.0.2
python-slugify==5.0.2
pytz==2021.3
PyYAML==5.4.1
pyzmq==22.3.0
qtconsole==5.1.1
QtPy==1.11.2
querystring-parser==1.2.4
regex==2021.10.8
requests==2.26.0
requests-oauthlib==1.3.0
requests-unixsocket==0.2.0
rope==0.19.0
rsa==4.7.2
scikit-learn==0.24.2
scipy==1.7.1
seaborn==0.11.1
semver==2.13.0
Send2Trash==1.8.0
shap==0.39.0
simplejson==3.17.5
six==1.15.0
sklearn==0.0
slicer==0.0.7
smmap==4.0.0
sniffio==1.2.0
SQLAlchemy==1.4.25
sqlparse==0.4.2
squarify==0.4.3
starlette==0.14.2
statsmodels==0.12.2
strsimpy==0.2.0
tables==3.6.1
tabulate==0.8.9
tenacity==8.0.1
tensorboard==2.7.0
tensorboard-data-server==0.6.1
tensorboard-plugin-wit==1.8.0
tensorflow==2.6.0
tensorflow-addons==0.14.0
tensorflow-estimator==2.6.0
termcolor==1.1.0
terminado==0.12.1
testpath==0.5.0
text-unidecode==1.3
threadpoolctl==2.2.0
toml==0.10.2
toposort==1.7
tornado==6.1
tqdm==4.62.3
traitlets==5.1.0
typeguard==2.13.0
typing-extensions==3.7.4.3
typing-inspect==0.7.1
urllib3==1.26.7
uvicorn==0.13.4
watchgod==0.7
wcwidth==0.2.5
webencodings==0.5.1
websocket-client==1.2.1
Werkzeug==2.0.2
widgetsnbextension==3.5.1
wrapt==1.12.1
xarray==0.19.0
xlrd==1.2.0
XlsxWriter==1.4.5
zipp==3.6.0
Hi @fguimara ,
I didn't get exactly the same error message (full code is not attached) but I think that something is wrong with your code snippet. Output shape should match num_classes
so I expect that you'd have model.add(keras.layers.Dense(2, activation=activation))
for num_classes=2
.
Does it help?
Have you tried to set the average
arg:
https://www.tensorflow.org/addons/api_docs/python/tfa/metrics/F1Score
Well, I have two classes, that can be mapped in a single neuron (Outputs -1 and 1), so I don't think there is a problem on this point. The Precision and Recall Callback classes work as a charm.
import numpy as np
import mlflow
import pandas as pd
from typing import List, Any, Tuple
# Tensorflow
import tensorflow as tf
#from tensorflow_addons.metrics import F1Score
tf.compat.v1.disable_v2_behavior()
from tensorflow import keras
from tensorflow.keras.callbacks import EarlyStopping
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import f1_score
# Plotting Resources
import matplotlib.pyplot as plt
import seaborn as sns
# Methods
def create_model(n_neurons: int=2,
input_size:int=16,
activation:str='tanh') -> keras.models.Sequential:
"""
Create MLP dense network
Arguments
---------
n_neurons: int, default=2
The number of neurons to be used in the hidden layer
input_size: int, default=16
The number of neurons in the input layers
activation: str, default='tanh'
Returns
-------
A `Sequential` model object
"""
model = keras.models.Sequential()
# add the input layers
model.add(keras.Input(shape=(input_size,)))
model.add(keras.layers.LayerNormalization())
# add hidden layers
model.add(keras.layers.Dense(n_neurons,
name="hidden_layer",
activation=activation))
# add output layer
model.add(keras.layers.Dense(1, activation=activation))
model.compile(loss='mse',
optimizer="rmsprop",
metrics=['accuracy',
F1Score(num_classes=2, average="weighted"),
keras.metrics.Precision(name='precision'),
keras.metrics.Recall(name='recall')])
return model
## MLP Training method
def mlp_train(X: pd.DataFrame,
y: pd.DataFrame) -> pd.DataFrame:
"""
Train a MLP network using backpropagation
"""
mlflow.tensorflow.autolog()
n_init = 1
input_size = X.shape[1]
y = y[X.index]
cv = X.shape[0]
early_stopping = EarlyStopping(monitor='f1', patience=100)
callbacks = [early_stopping,]
model = KerasClassifier(build_fn=create_model,
epochs=10000,
validation_split=.3,
input_size=input_size,
batch_size=cv//2,
verbose=0)
n_neurons = [2,]
param_grid = dict(
n_neurons=n_neurons * n_init,
)
grid = GridSearchCV(
estimator=model,
param_grid=param_grid,
n_jobs=-1,
scoring='f1',
refit=False,
verbose=2,
return_train_score=False,
cv=cv
)
grid_search_obj = grid.fit(X.values.astype('float32'),
y.values.astype('float32'),
callbacks=callbacks)
loo_results = pd.DataFrame(grid_search_obj.cv_results_)
return loo_results
Can you share a small google Colab to reproduce this?