Home > Back-end >  Tensorflow accuracy score
Tensorflow accuracy score

Time:05-05

I am creating a machine learning model in the form of regression. I started with XGBoost to get my first estimates. However, these were not convincing enough (using XGB Regressor I got only 60% accuracy).

So I started looking for solutions with neural networks and ended up using tensorflow. However, I am relatively new to this module and would like to know if there is an equivalent to xgboost.score?

The first code was using xgboost and I am working now on the second one, with tensorflow.

xgb = XGBRegressor(learning_rate = 0.30012, max_depth = 5, n_estimators = 180, subsample = 0.7, colsample_bylevel = 0.7, colsample_bytree = 0.7, min_child_weight = 4, reg_alpha = 10, reg_lambda = 10)
xgb.fit(X_train, y_train)

print("Score on train data : "   str(xgb.score(X_train, y_train)))
print("Score on validation data : "   str(xgb.score(X_val, y_val)))

df_test['RUL'] = xgb.predict(X_test)
df_sub = df_test.groupby('engine_no').agg({'RUL': 'last'}).reset_index()
df_sub[['engine_no','RUL']].to_csv('submission.csv', index=False)

print(df_sub)

The second one using TensorFlow:

tf.random.set_seed(123)  #first we set random seed

model = tf.keras.Sequential([
  tf.keras.layers.Dense(100, activation = tf.keras.activations.relu),
  tf.keras.layers.Dense(10),
  tf.keras.layers.Dense(1)
])

model.compile( loss = tf.keras.losses.mae, #mae stands for mean absolute error
              optimizer = tf.keras.optimizers.SGD(), #stochastic GD
              metrics = ['mae'])
model.fit( X_train, y_train, epochs = 100)

###############################################################################
print("Score on train data : "   str(xgb.score(X_train, y_train))) #I want .score here but with TF
print("Score on validation data : "   str(xgb.score(X_val, y_val))) #Same here

CodePudding user response:

xgb.score returns the R2 score, you can implement this metric in tensorflow from scratch,

def R_squared(y, y_pred):
  residual = tf.reduce_sum(tf.square(tf.subtract(y, y_pred)))
  total = tf.reduce_sum(tf.square(tf.subtract(y, tf.reduce_mean(y))))
  r2 = tf.subtract(1.0, tf.div(residual, total))
  return r2

When compiling your model, pass this function in the metrics parameter so that it shows up when evaluating and training your model, like this:

model.compile( loss = tf.keras.losses.mae, #mae stands for mean absolute error
              optimizer = tf.keras.optimizers.SGD(), #stochastic GD
              metrics = ['mae', R_squared])

When you evaluate your model using model.evaluate, as in below, the R2 score will appear,

y_pred = model.predict(X_val)
model.evaluate(y_pred, y_val)

CodePudding user response:

  • Related