import{s as pl,n as yl,o as il}from"../chunks/scheduler.5f3e6389.js";import{S as cl,i as rl,e as o,s as e,c as M,h as Jl,a as m,d as a,b as n,f as nl,g as p,j as X,k as Ml,l as ol,m as t,n as y,t as i,o as c,p as r}from"../chunks/index.62df735e.js";import{C as ml,H as P,E as wl}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.e306bd5b.js";import{C as z}from"../chunks/CodeBlock.dc1e8be0.js";function jl(O){let J,g,G,x,w,Y,j,E,U,ll="Evaluate can be easily intergrated into your Keras and Tensorflow workflow. We’ll demonstrate two ways of incorporating Evaluate into model training, using the Fashion MNIST example dataset. We’ll train a standard classifier to predict two classes from this dataset, and show how to use a metric as a callback during training or afterwards for evaluation.",R,h,Q,T,S,d,sl="Suppose we want to keep track of model metrics while a model is training. We can use a Callback in order to calculate this metric during training, after an epoch ends.",N,u,al="We’ll define a callback here that will take a metric name and our training data, and have it calculate a metric after the epoch ends.",$,b,A,f,tl="We can pass this class to the <code>callbacks</code> keyword-argument to use it during training:",v,C,H,I,F,_,el="We can also use the same metric after model training! Here, we show how to check accuracy of the model after training on the test set:",V,k,K,Z,D,B,q,W,L;return w=new ml({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"}}),j=new P({props:{title:"Working with Keras and Tensorflow",local:"working-with-keras-and-tensorflow",headingTag:"h1"}}),h=new z({props:{code:"aW1wb3J0JTIwbnVtcHklMjBhcyUyMG5wJTBBZnJvbSUyMHRlbnNvcmZsb3clMjBpbXBvcnQlMjBrZXJhcyUwQWZyb20lMjB0ZW5zb3JmbG93LmtlcmFzJTIwaW1wb3J0JTIwbGF5ZXJzJTBBaW1wb3J0JTIwZXZhbHVhdGUlMEElMEElMjMlMjBXZSUyMHB1bGwlMjBleGFtcGxlJTIwY29kZSUyMGZyb20lMjBLZXJhcy5pbydzJTIwZ3VpZGUlMjBvbiUyMGNsYXNzaWZ5aW5nJTIwd2l0aCUyME1OSVNUJTBBJTIzJTIwTG9jYXRl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MjAlMjAlMjAlMjAlMjAlMjBsYXllcnMuRmxhdHRlbigpJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwbGF5ZXJzLkRyb3BvdXQoMC41KSUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGxheWVycy5EZW5zZSgxJTJDJTIwYWN0aXZhdGlvbiUzRCUyMnNpZ21vaWQlMjIpJTJDJTBBJTIwJTIwJTIwJTIwJTVEJTBBKQ==",highlighted:`<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
<span class="hljs-keyword">from</span> tensorflow <span class="hljs-keyword">import</span> keras
<span class="hljs-keyword">from</span> tensorflow.keras <span class="hljs-keyword">import</span> layers
<span class="hljs-keyword">import</span> evaluate

<span class="hljs-comment"># We pull example code from Keras.io&#x27;s guide on classifying with MNIST</span>
<span class="hljs-comment"># Located here: https://keras.io/examples/vision/mnist_convnet/</span>

<span class="hljs-comment"># Model / data parameters</span>
input_shape = (<span class="hljs-number">28</span>, <span class="hljs-number">28</span>, <span class="hljs-number">1</span>)

<span class="hljs-comment"># Load the data and split it between train and test sets</span>
(x_train, y_train), (x_test, y_test) = keras.datasets.fashion_mnist.load_data()


<span class="hljs-comment"># Only select tshirts/tops and trousers, classes 0 and 1</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">get_tshirts_tops_and_trouser</span>(<span class="hljs-params">x_vals, y_vals</span>):
    mask = np.where((y_vals == <span class="hljs-number">0</span>) | (y_vals == <span class="hljs-number">1</span>))
    <span class="hljs-keyword">return</span> x_vals[mask], y_vals[mask]

x_train, y_train = get_tshirts_tops_and_trouser(x_train, y_train)
x_test, y_test = get_tshirts_tops_and_trouser(x_test, y_test)


<span class="hljs-comment"># Scale images to the [0, 1] range</span>
x_train = x_train.astype(<span class="hljs-string">&quot;float32&quot;</span>) / <span class="hljs-number">255</span>
x_test = x_test.astype(<span class="hljs-string">&quot;float32&quot;</span>) / <span class="hljs-number">255</span>

x_train = np.expand_dims(x_train, -<span class="hljs-number">1</span>)
x_test = np.expand_dims(x_test, -<span class="hljs-number">1</span>)


model = keras.Sequential(
    [
        keras.Input(shape=input_shape),
        layers.Conv2D(<span class="hljs-number">32</span>, kernel_size=(<span class="hljs-number">3</span>, <span class="hljs-number">3</span>), activation=<span class="hljs-string">&quot;relu&quot;</span>),
        layers.MaxPooling2D(pool_size=(<span class="hljs-number">2</span>, <span class="hljs-number">2</span>)),
        layers.Conv2D(<span class="hljs-number">64</span>, kernel_size=(<span class="hljs-number">3</span>, <span class="hljs-number">3</span>), activation=<span class="hljs-string">&quot;relu&quot;</span>),
        layers.MaxPooling2D(pool_size=(<span class="hljs-number">2</span>, <span class="hljs-number">2</span>)),
        layers.Flatten(),
        layers.Dropout(<span class="hljs-number">0.5</span>),
        layers.Dense(<span class="hljs-number">1</span>, activation=<span class="hljs-string">&quot;sigmoid&quot;</span>),
    ]
)`,wrap:!1}}),T=new P({props:{title:"Callbacks",local:"callbacks",headingTag:"h2"}}),b=new z({props:{code:"Y2xhc3MlMjBNZXRyaWNzQ2FsbGJhY2soa2VyYXMuY2FsbGJhY2tzLkNhbGxiYWNrKSUzQSUwQSUwQSUyMCUyMCUyMCUyMGRlZiUyMF9faW5pdF9fKHNlbGYlMkMlMjBtZXRyaWNfbmFtZSUyQyUyMHhfZGF0YSUyQyUyMHlfZGF0YSklMjAtJTNFJTIwTm9uZSUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHN1cGVyKE1ldHJpY3NDYWxsYmFjayUyQyUyMHNlbGYpLl9faW5pdF9fKCklMEElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBzZWxmLnhfZGF0YSUyMCUzRCUyMHhfZGF0YSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHNlbGYueV9kYXRhJTIwJTNEJTIweV9kYXRhJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwc2VsZi5tZXRyaWNfbmFtZSUyMCUzRCUyMG1ldHJpY19uYW1lJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwc2VsZi5tZXRyaWMlMjAlM0QlMjBldmFsdWF0ZS5sb2FkKG1ldHJpY19uYW1lKSUwQSUwQSUyMCUyMCUyMCUyMGRlZiUyMG9uX2Vwb2NoX2VuZChzZWxmJTJDJTIwZXBvY2glMkMlMjBsb2dzJTNEZGljdCgpKSUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMG0lMjAlM0QlMjBzZWxmLm1vZGVsJTIwJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIzJTIwRW5zdXJlJTIwd2UlMjBnZXQlMjBsYWJlbHMlMjBvZiUyMCUyMjElMjIlMjBvciUyMCUyMjAlMjIlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjB0cmFpbmluZ19wcmVkcyUyMCUzRCUyMG5wLnJvdW5kKG0ucHJlZGljdChzZWxmLnhfZGF0YSkpJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwdHJhaW5pbmdfbGFiZWxzJTIwJTNEJTIwc2VsZi55X2RhdGElMEElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjMlMjBDb21wdXRlJTIwc2NvcmUlMjBhbmQlMjBzYXZlJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwc2NvcmUlMjAlM0QlMjBzZWxmLm1ldHJpYy5jb21wdXRlKHByZWRpY3Rpb25zJTIwJTNEJTIwdHJhaW5pbmdfcHJlZHMlMkMlMjByZWZlcmVuY2VzJTIwJTNEJTIwdHJhaW5pbmdfbGFiZWxzKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGxvZ3MudXBkYXRlKHNjb3JlKQ==",highlighted:`<span class="hljs-keyword">class</span> <span class="hljs-title class_">MetricsCallback</span>(keras.callbacks.Callback):

    <span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, metric_name, x_data, y_data</span>) -&gt; <span class="hljs-literal">None</span>:
        <span class="hljs-built_in">super</span>(MetricsCallback, self).__init__()

        self.x_data = x_data
        self.y_data = y_data
        self.metric_name = metric_name
        self.metric = evaluate.load(metric_name)

    <span class="hljs-keyword">def</span> <span class="hljs-title function_">on_epoch_end</span>(<span class="hljs-params">self, epoch, logs=<span class="hljs-built_in">dict</span>(<span class="hljs-params"></span>)</span>):
        m = self.model 
        <span class="hljs-comment"># Ensure we get labels of &quot;1&quot; or &quot;0&quot;</span>
        training_preds = np.<span class="hljs-built_in">round</span>(m.predict(self.x_data))
        training_labels = self.y_data

        <span class="hljs-comment"># Compute score and save</span>
        score = self.metric.compute(predictions = training_preds, references = training_labels)
        
        logs.update(score)`,wrap:!1}}),C=new z({props:{code:"YmF0Y2hfc2l6ZSUyMCUzRCUyMDEyOCUwQWVwb2NocyUyMCUzRCUyMDIlMEElMEFtb2RlbC5jb21waWxlKGxvc3MlM0QlMjJiaW5hcnlfY3Jvc3NlbnRyb3B5JTIyJTJDJTIwb3B0aW1pemVyJTNEJTIyYWRhbSUyMiklMEElMEFtb2RlbF9oaXN0b3J5JTIwJTNEJTIwbW9kZWwuZml0KHhfdHJhaW4lMkMlMjB5X3RyYWluJTJDJTIwYmF0Y2hfc2l6ZSUzRGJhdGNoX3NpemUlMkMlMjBlcG9jaHMlM0RlcG9jaHMlMkMlMjB2YWxpZGF0aW9uX3NwbGl0JTNEMC4xJTJDJTIwJTBBY2FsbGJhY2tzJTIwJTNEJTIwJTVCTWV0cmljc0NhbGxiYWNrKHhfZGF0YSUyMCUzRCUyMHhfdHJhaW4lMkMlMjB5X2RhdGElMjAlM0QlMjB5X3RyYWluJTJDJTIwbWV0cmljX25hbWUlMjAlM0QlMjAlMjJhY2N1cmFjeSUyMiklNUQp",highlighted:`batch_size = <span class="hljs-number">128</span>
epochs = <span class="hljs-number">2</span>

model.<span class="hljs-built_in">compile</span>(loss=<span class="hljs-string">&quot;binary_crossentropy&quot;</span>, optimizer=<span class="hljs-string">&quot;adam&quot;</span>)

model_history = model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=<span class="hljs-number">0.1</span>, 
callbacks = [MetricsCallback(x_data = x_train, y_data = y_train, metric_name = <span class="hljs-string">&quot;accuracy&quot;</span>)])`,wrap:!1}}),I=new P({props:{title:"Using an Evaluate Metric for… Evaluation!",local:"using-an-evaluate-metric-for-evaluation",headingTag:"h2"}}),k=new z({props:{code:"YWNjJTIwJTNEJTIwZXZhbHVhdGUubG9hZCglMjJhY2N1cmFjeSUyMiklMEElMjMlMjBSb3VuZCUyMHRoZSUyMHByZWRpY3Rpb25zJTIwdG8lMjB0dXJuJTIwdGhlbSUyMGludG8lMjAlMjIwJTIyJTIwb3IlMjAlMjIxJTIyJTIwbGFiZWxzJTBBdGVzdF9wcmVkcyUyMCUzRCUyMG5wLnJvdW5kKG1vZGVsLnByZWRpY3QoeF90ZXN0KSklMEF0ZXN0X2xhYmVscyUyMCUzRCUyMHlfdGVzdA==",highlighted:`acc = evaluate.load(<span class="hljs-string">&quot;accuracy&quot;</span>)
<span class="hljs-comment"># Round the predictions to turn them into &quot;0&quot; or &quot;1&quot; labels</span>
test_preds = np.<span class="hljs-built_in">round</span>(model.predict(x_test))
test_labels = y_test`,wrap:!1}}),Z=new z({props:{code:"cHJpbnQoJTIyVGVzdCUyMGFjY3VyYWN5JTIwaXMlMjAlM0ElMjAlMjIlMkMlMjBhY2MuY29tcHV0ZShwcmVkaWN0aW9ucyUyMCUzRCUyMHRlc3RfcHJlZHMlMkMlMjByZWZlcmVuY2VzJTIwJTNEJTIwdGVzdF9sYWJlbHMpKSUwQSUyMyUyMFRlc3QlMjBhY2N1cmFjeSUyMGlzJTIwJTNBJTIwMC45ODU1",highlighted:`<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Test accuracy is : &quot;</span>, acc.compute(predictions = test_preds, references = test_labels))
<span class="hljs-comment"># Test accuracy is : 0.9855</span>`,wrap:!1}}),B=new wl({props:{source:"https://github.com/huggingface/evaluate/blob/main/docs/source/keras_integrations.md"}}),{c(){J=o("meta"),g=e(),G=o("p"),x=e(),M(w.$$.fragment),Y=e(),M(j.$$.fragment),E=e(),U=o("p"),U.textContent=ll,R=e(),M(h.$$.fragment),Q=e(),M(T.$$.fragment),S=e(),d=o("p"),d.textContent=sl,N=e(),u=o("p"),u.textContent=al,$=e(),M(b.$$.fragment),A=e(),f=o("p"),f.innerHTML=tl,v=e(),M(C.$$.fragment),H=e(),M(I.$$.fragment),F=e(),_=o("p"),_.textContent=el,V=e(),M(k.$$.fragment),K=e(),M(Z.$$.fragment),D=e(),M(B.$$.fragment),q=e(),W=o("p"),this.h()},l(l){const s=Jl("svelte-u9bgzb",document.head);J=m(s,"META",{name:!0,content:!0}),s.forEach(a),g=n(l),G=m(l,"P",{}),nl(G).forEach(a),x=n(l),p(w.$$.fragment,l),Y=n(l),p(j.$$.fragment,l),E=n(l),U=m(l,"P",{"data-svelte-h":!0}),X(U)!=="svelte-dqtpg8"&&(U.textContent=ll),R=n(l),p(h.$$.fragment,l),Q=n(l),p(T.$$.fragment,l),S=n(l),d=m(l,"P",{"data-svelte-h":!0}),X(d)!=="svelte-1vmwzbw"&&(d.textContent=sl),N=n(l),u=m(l,"P",{"data-svelte-h":!0}),X(u)!=="svelte-1125oxi"&&(u.textContent=al),$=n(l),p(b.$$.fragment,l),A=n(l),f=m(l,"P",{"data-svelte-h":!0}),X(f)!=="svelte-1ba9645"&&(f.innerHTML=tl),v=n(l),p(C.$$.fragment,l),H=n(l),p(I.$$.fragment,l),F=n(l),_=m(l,"P",{"data-svelte-h":!0}),X(_)!=="svelte-18qazw3"&&(_.textContent=el),V=n(l),p(k.$$.fragment,l),K=n(l),p(Z.$$.fragment,l),D=n(l),p(B.$$.fragment,l),q=n(l),W=m(l,"P",{}),nl(W).forEach(a),this.h()},h(){Ml(J,"name","hf:doc:metadata"),Ml(J,"content",Ul)},m(l,s){ol(document.head,J),t(l,g,s),t(l,G,s),t(l,x,s),y(w,l,s),t(l,Y,s),y(j,l,s),t(l,E,s),t(l,U,s),t(l,R,s),y(h,l,s),t(l,Q,s),y(T,l,s),t(l,S,s),t(l,d,s),t(l,N,s),t(l,u,s),t(l,$,s),y(b,l,s),t(l,A,s),t(l,f,s),t(l,v,s),y(C,l,s),t(l,H,s),y(I,l,s),t(l,F,s),t(l,_,s),t(l,V,s),y(k,l,s),t(l,K,s),y(Z,l,s),t(l,D,s),y(B,l,s),t(l,q,s),t(l,W,s),L=!0},p:yl,i(l){L||(i(w.$$.fragment,l),i(j.$$.fragment,l),i(h.$$.fragment,l),i(T.$$.fragment,l),i(b.$$.fragment,l),i(C.$$.fragment,l),i(I.$$.fragment,l),i(k.$$.fragment,l),i(Z.$$.fragment,l),i(B.$$.fragment,l),L=!0)},o(l){c(w.$$.fragment,l),c(j.$$.fragment,l),c(h.$$.fragment,l),c(T.$$.fragment,l),c(b.$$.fragment,l),c(C.$$.fragment,l),c(I.$$.fragment,l),c(k.$$.fragment,l),c(Z.$$.fragment,l),c(B.$$.fragment,l),L=!1},d(l){l&&(a(g),a(G),a(x),a(Y),a(E),a(U),a(R),a(Q),a(S),a(d),a(N),a(u),a($),a(A),a(f),a(v),a(H),a(F),a(_),a(V),a(K),a(D),a(q),a(W)),a(J),r(w,l),r(j,l),r(h,l),r(T,l),r(b,l),r(C,l),r(I,l),r(k,l),r(Z,l),r(B,l)}}}const Ul='{"title":"Working with Keras and Tensorflow","local":"working-with-keras-and-tensorflow","sections":[{"title":"Callbacks","local":"callbacks","sections":[],"depth":2},{"title":"Using an Evaluate Metric for… Evaluation!","local":"using-an-evaluate-metric-for-evaluation","sections":[],"depth":2}],"depth":1}';function hl(O){return il(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class fl extends cl{constructor(J){super(),rl(this,J,hl,jl,pl,{})}}export{fl as component};
