Exemplo: GAN de animais em blocos; plot sem estado duplicado

examples/gan-animais: GAN condicional (DCGAN) de gatos, caes e coelhos do
CIFAR-10/100, toda em blocos: baixa por HTTPS, extrai, separa as classes,
treina com Adam e mostra 24 amostras e a curva das perdas (tinyblocks-plot)
a cada 20 passos; grava os parametros a cada 500. gan.sb (notacao do
forum), gan.json (projeto) e README.

plot: as funcoes publicas so chamam as static. No editor a library vem
linkada e tambem como .so; dentro do .so a chamada a tb_plot_render ia
para a copia do executavel, que tinha outra figura (o titulo e as series
sumiam).

Pendencias: parametros de camada dependem da profundidade de chamada de
Meus Blocos; campo de texto do bloco corta em 63 caracteres.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mguinhosandClaude Opus 5.5 committed 2026-10-27 00:38:33 -03:00
1 parent 7538d48a13
commit 422c3fb6cb
5 files changed
+18544 -6

No files matched your search

+13 -6
View File
@@ -62,7 +62,10 @@ static void series_free(Series* s) {
free(s->names);
}
void tb_plot_figure(int w, int h) {
/* As funcoes publicas (tb_plot_*) so chamam as static: dentro do .so uma
chamada a funcao exportada pode ir para a copia linkada no executavel
(o editor tem as duas), que tem outra figura atual. */
static void figure_reset(int w, int h) {
for (int i = 0; i < F.n; i++) series_free(&F.s[i]);
F.n = 0;
F.w = w < 64 ? 64 : w > 4096 ? 4096 : w;
@@ -93,6 +96,8 @@ static Series* add(int kind, const float* x, const float* y, int n, const char*
return s;
}
void tb_plot_figure(int w, int h) { figure_reset(w, h); }
void tb_plot_line(const float* x, const float* y, int n, const char* color, const char* label) {
add(S_LINE, x, y, n, color, label);
}
@@ -237,7 +242,7 @@ static void bounds(double* x0, double* x1, double* y0, double* y1) {
if (F.ylim) { *y0 = F.y0; *y1 = F.y1; }
}
TinyBlocks_Image* tb_plot_render(void) {
static TinyBlocks_Image* render(void) {
int W = F.w, H = F.h;
TinyBlocks_Image* img = tb_image_new(W, H, 4);
if (!img) return NULL;
@@ -350,6 +355,8 @@ TinyBlocks_Image* tb_plot_render(void) {
return img;
}
TinyBlocks_Image* tb_plot_render(void) { return render(); }
/* == Valores ========================================================== */
/* Numeros de um valor: tensor, lista ou texto "1, 2, 3"; malloc ou NULL */
static float* numbers(TinyBlocks* rt, TinyBlocks_Value v, int* n) {
@@ -452,7 +459,7 @@ static Named* named(const char* name, bool create) {
/* == Blocos =========================================================== */
static void eval_figure(TinyBlocks_Block* self, TinyBlocks* rt) {
tb_plot_figure((int)tb_block_input_integer(self, rt, 0, 480), (int)tb_block_input_integer(self, rt, 1, 320));
figure_reset((int)tb_block_input_integer(self, rt, 0, 480), (int)tb_block_input_integer(self, rt, 1, 320));
self->dirty = false;
}
@@ -576,14 +583,14 @@ static void eval_series_len(TinyBlocks_Block* self, TinyBlocks* rt) {
static void eval_image(TinyBlocks_Block* self, TinyBlocks* rt) {
(void)rt;
TinyBlocks_Image* img = tb_plot_render();
TinyBlocks_Image* img = render();
if (img) { tb_block_set_output(self, 0, tb_value_image(img)); tb_image_release(img); }
self->dirty = false;
}
static void eval_save(TinyBlocks_Block* self, TinyBlocks* rt) {
const char* path = tb_block_input_text(self, rt, 0);
TinyBlocks_Image* img = tb_plot_render();
TinyBlocks_Image* img = render();
if (!img || !tb_image_save(img, path))
tinyblocks_set_error(rt, "não gravou '%s' (o formato da extensão está carregado?)", path);
tb_image_release(img);
@@ -652,7 +659,7 @@ static bool plot_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
static void plot_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
(void)rt; (void)lib;
tb_plot_figure(480, 320);
figure_reset(480, 320);
free(F.s); F.s = NULL; F.cap = 0;
for (int i = 0; i < s_named_n; i++) free(s_named[i].v);
free(s_named); s_named = NULL; s_named_n = 0;