File size: 11,595 Bytes
7100bee |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"import gpt_2_simple as gpt2\n",
"import os\n",
"import tensorflow as tf\n",
"import pandas as pd\n",
"import re\n",
"print(\"GPU is\", \"available\" if tf.test.is_gpu_available() else \"NOT AVAILABLE\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model_name = \"124M\"\n",
"if not os.path.isdir(os.path.join(\"models\", model_name)):\n",
"\tprint(f\"Downloading {model_name} model...\")\n",
"\tgpt2.download_gpt2(model_name=model_name) "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"path = 'AbstractGenerator/'\n",
"checkpoint_dir =path+'weights/'\n",
"data_path = path+'Tokenized_data/'\n",
"\n",
"\n",
"file_name = 'resumen'\n",
"file_path = data_path+file_name\n",
"\n",
"prefix= '<|startoftext|>'\n",
"sufix ='<|endoftext|>'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# pretrained"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sess = gpt2.start_tf_sess()\n",
"gpt2.load_gpt2(sess,checkpoint_dir=checkpoint_dir,run_name='run1')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# train "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tf.compat.v1.reset_default_graph()\n",
"sess = gpt2.start_tf_sess()\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"gpt2.finetune(sess,\n",
" file_path+'.txt',\n",
" model_name=model_name,\n",
" checkpoint_dir=checkpoint_dir, \n",
" steps=1000\n",
" ) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# test"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"text = \"\"\"Introduction and preliminaries\n",
"The focus of this paper is decompositions of (k, `)-sparse graphs into edge-disjoint subgraphs\n",
"that certify sparsity. We use graph to mean a multigraph, possibly with loops. We say that a\n",
"graph is (k, `)-sparse if no subset of n′ vertices spans more than kn′− ` edges in the graph; a\n",
"(k, `)-sparse graph with kn′− ` edges is (k, `)-tight. We call the range k ≤ `≤ 2k−1 the upper\n",
"range of sparse graphs and 0≤ `≤ k the lower range.\n",
"In this paper, we present efficient algorithms for finding decompositions that certify sparsity\n",
"in the upper range of `. Our algorithms also apply in the lower range, which was already ad-\n",
"dressed by [3, 4, 5, 6, 19]. A decomposition certifies the sparsity of a graph if the sparse graphs\n",
"and graphs admitting the decomposition coincide.\n",
"Our algorithms are based on a new characterization of sparse graphs, which we call the\n",
"pebble game with colors. The pebble game with colors is a simple graph construction rule that\n",
"produces a sparse graph along with a sparsity-certifying decomposition.\n",
"We define and study a canonical class of pebble game constructions, which correspond to\n",
"previously studied decompositions of sparse graphs into edge disjoint trees. Our results provide\n",
"a unifying framework for all the previously known special cases, including Nash-Williams-\n",
"Tutte and [7, 24]. Indeed, in the lower range, canonical pebble game constructions capture the\n",
"properties of the augmenting paths used in matroid union and intersection algorithms[5, 6].\n",
"Since the sparse graphs in the upper range are not known to be unions or intersections of the\n",
"matroids for which there are efficient augmenting path algorithms, these do not easily apply in\n",
"∗ Research of both authors funded by the NSF under grants NSF CCF-0430990 and NSF-DARPA CARGO\n",
"CCR-0310661 to the first author.\n",
"2 Ileana Streinu, Louis Theran\n",
"Term Meaning\n",
"Sparse graph G Every non-empty subgraph on n′ vertices has ≤ kn′− ` edges\n",
"Tight graph G G = (V,E) is sparse and |V |= n, |E|= kn− `\n",
"Block H in G G is sparse, and H is a tight subgraph\n",
"Component H of G G is sparse and H is a maximal block\n",
"Map-graph Graph that admits an out-degree-exactly-one orientation\n",
"(k, `)-maps-and-trees Edge-disjoint union of ` trees and (k− `) map-grpahs\n",
"`Tk Union of ` trees, each vertex is in exactly k of them\n",
"Set of tree-pieces of an `Tk induced on V ′ ⊂V Pieces of trees in the `Tk spanned by E(V ′)\n",
"Proper `Tk Every V ′ ⊂V contains ≥ ` pieces of trees from the `Tk\n",
"Table 1. Sparse graph and decomposition terminology used in this paper.\n",
"the upper range. Pebble game with colors constructions may thus be considered a strengthening\n",
"of augmenting paths to the upper range of matroidal sparse graphs.\n",
"1.1. Sparse graphs\n",
"\n",
"ABSTRACT\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Introduction and preliminaries\n",
"The focus of this paper is decompositions of (k, `)-sparse graphs into edge-disjoint subgraphs\n",
"that certify sparsity. We use graph to mean a multigraph, possibly with loops. We say that a\n",
"graph is (k, `)-sparse if no subset of n′ vertices spans more than kn′− ` edges in the graph; a\n",
"(k, `)-sparse graph with kn′− ` edges is (k, `)-tight. We call the range k ≤ `≤ 2k−1 the upper\n",
"range of sparse graphs and 0≤ `≤ k the lower range.\n",
"In this paper, we present efficient algorithms for finding decompositions that certify sparsity\n",
"in the upper range of `. Our algorithms also apply in the lower range, which was already ad-\n",
"dressed by [3, 4, 5, 6, 19]. A decomposition certifies the sparsity of a graph if the sparse graphs\n",
"and graphs admitting the decomposition coincide.\n",
"Our algorithms are based on a new characterization of sparse graphs, which we call the\n",
"pebble game with colors. The pebble game with colors is a simple graph construction rule that\n",
"produces a sparse graph along with a sparsity-certifying decomposition.\n",
"We define and study a canonical class of pebble game constructions, which correspond to\n",
"previously studied decompositions of sparse graphs into edge disjoint trees. Our results provide\n",
"a unifying framework for all the previously known special cases, including Nash-Williams-\n",
"Tutte and [7, 24]. Indeed, in the lower range, canonical pebble game constructions capture the\n",
"properties of the augmenting paths used in matroid union and intersection algorithms[5, 6].\n",
"Since the sparse graphs in the upper range are not known to be unions or intersections of the\n",
"matroids for which there are efficient augmenting path algorithms, these do not easily apply in\n",
"∗ Research of both authors funded by the NSF under grants NSF CCF-0430990 and NSF-DARPA CARGO\n",
"CCR-0310661 to the first author.\n",
"2 Ileana Streinu, Louis Theran\n",
"Term Meaning\n",
"Sparse graph G Every non-empty subgraph on n′ vertices has ≤ kn′− ` edges\n",
"Tight graph G G = (V,E) is sparse and |V |= n, |E|= kn− `\n",
"Block H in G G is sparse, and H is a tight subgraph\n",
"Component H of G G is sparse and H is a maximal block\n",
"Map-graph Graph that admits an out-degree-exactly-one orientation\n",
"(k, `)-maps-and-trees Edge-disjoint union of ` trees and (k− `) map-grpahs\n",
"`Tk Union of ` trees, each vertex is in exactly k of them\n",
"Set of tree-pieces of an `Tk induced on V ′ ⊂V Pieces of trees in the `Tk spanned by E(V ′)\n",
"Proper `Tk Every V ′ ⊂V contains ≥ ` pieces of trees from the `Tk\n",
"Table 1. Sparse graph and decomposition terminology used in this paper.\n",
"the upper range. Pebble game with colors constructions may thus be considered a strengthening\n",
"of augmenting paths to the upper range of matroidal sparse graphs.\n",
"1.1. Sparse graphs\n",
"\n",
"ABSTRACT\n",
" Here we show that decompositions of sparse graphs are sometimes more efficient than\n",
"combinations of adjacent trees. In this paper, we apply a combinatorial\n",
"analysis of SparseGraph.com [5, 6] to evaluate our algorithm for finding\n",
"appropriate decompositions of sparse graphs in the upper range of sparse graphs\n",
"and in the lower range of sparse graphs. We show that the decomposition\n",
"certified in SparseGraph.com is as accurate as a tree-building algorithm\n",
"in the upper range of sparse graphs. This demonstrates that for the\n",
"case of SparseGraph.com [5], it is possible to construct a suitable decomposition\n",
"class within the bounds of the graph. The algorithm we use is based on a\n",
"combination of decompositions.\n",
"\n",
"\n"
]
}
],
"source": [
"gpt2.generate(sess,prefix=text,truncate=sufix,checkpoint_dir=checkpoint_dir,nsamples=1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Data Tokeniser"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ds = pd.read_csv('Recipe-Creator\\data\\scientific_paper_full_text_translated.csv')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import codecs\n",
"with codecs.open(\"Recipe-Creator/Tokenized_data/resumen.txt\",'a','utf-8') as f:\n",
" for i in ds.index:\n",
" f.write(prefix+\"\\n\")\n",
" f.write(ds.iloc[i]['text_no_abstract'])\n",
" f.write(\"ABSTRACT\\n\")\n",
" f.write(ds.iloc[i]['abstract']+\"\\n\")\n",
" f.write(sufix)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import gradio as gr\n",
"\n",
"def greet(text):\n",
" return gpt2.generate(sess,prefix=str(text),truncate=sufix,checkpoint_dir=checkpoint_dir,nsamples=1)\n",
"\n",
"iface = gr.Interface(fn=greet, inputs=\"text\", outputs=\"text\")\n",
"iface.launch(share=True,debug=True)"
]
}
],
"metadata": {
"interpreter": {
"hash": "53fbdc69e3e12c371950068c144423682c30d04ec68c2bd46937202e33e0058d"
},
"kernelspec": {
"display_name": "Python 3.7.11 ('receta')",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.11"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
|